inspection
Array.prototype.concat
Returns a new array formed by joining the source with the provided arrays and/or values (one level of spread). Reach for it to merge arrays without mutating any of them.
arr.concat(...values) Parameters
| Parameter | Purpose |
|---|---|
| ...values | Arrays (spread one level) or plain values to append |
| return value | A new array; original arrays are unchanged (non-mutating) |
Examples
console.log([1,2].concat([3,4])); Logs: [1, 2, 3, 4]
console.log([1].concat(2, [3, 4], 5)); Logs: [1, 2, 3, 4, 5] (mixes values and arrays)
console.log([1].concat([[2, 3]])); Logs: [1, [2, 3]] (only one level of spread)
const a = [1,2]; const b = [...a, 3, 4]; console.log(b); Logs: [1, 2, 3, 4] (spread syntax is the modern equivalent)
Gotcha
Only spreads ONE level — nested arrays inside the arguments stay nested. Spread syntax `[...a, ...b]` is the modern replacement in most codebases.
Related methods
Array.prototype.slice
Returns a shallow copy of a portion of the array from start (inclusive) to end (exclusive) without modifying the source. Reach for it to extract a range or to clone an array (`arr.slice()`).
Array.prototype.flat
Returns a new array with all sub-array elements concatenated up to the specified depth (ES2019). Reach for it to unnest arrays of arrays without a manual reduce.
Array.prototype.push
Appends one or more elements to the end of the array and returns the new length. Reach for it to grow a list — it's the canonical "add to the end" call.
Array.prototype.at
Returns the element at the given integer index, supporting negative values that count from the end (ES2022). Reach for it as the clean replacement for `arr[arr.length - 1]`.
Array.prototype.join
Returns a string formed by concatenating each element, separated by the given string (default ','). Reach for it to build CSV/URL/paths or any delimited string from an array.