jsrosetta

Collections

Arrays

Node.js's slice and concat compared to Go's slice, Rust's Vec, Swift's value-type Array, and Java's ArrayList.

Minimum versions
Node.js ≥ 12.20Go ≥ 1.21Rust ≥ 1.58Swift ≥ 2.0Java ≥ 25
Verified on
Node.js 24.12.0Go 1.27.1Rust 1.98.1Swift 6.2.4Java 25.0.4.1

Node.js code is an ES module: save it as .mjs or set "type": "module" in package.json.

In Node.js, Array is a flexible reference type with plenty of built-in methods. Go uses a slice — a view (pointer, length, capacity) into an underlying array — so slicing, cloning, and concatenating all require you to think about whether the operation shares memory with the original. Rust has Vec<T>, which owns its data, and a borrowed slice type (&[T]) — the compiler enforces the ownership boundary. Swift's Array is a value type: assigning or passing it makes a logical copy (copy-on-write), unlike JS's implicit sharing. Java has no resizable array type — int[] is fixed-size — so the example below uses ArrayList.

Cloning and slicing

const array = [1, 2, 3, 4, 5]
console.log(array)
 
const clone = array.slice(0) // slice() always returns a new array
console.log(clone)
 
const sub = array.slice(2, 4)
console.log(sub) // [3, 4]

Concatenating and prepending

const concatenated = clone.concat([6, 7])
console.log(concatenated) // [1, 2, 3, 4, 5, 6, 7]
 
const prepended = [-2, -1, 0].concat(concatenated)
console.log(prepended) // [-2, -1, 0, 1, 2, 3, 4, 5, 6, 7]

Reference: github.com/miguelmota/golang-for-nodejs-developers#arrays