Merge arrays immutably
Combine two or more lists into one without changing the originals.
JavaScript · Function
Array.prototype.concat() returns a new array formed from the calling array and supplied arrays or values. It does not mutate the source arrays and spreads array arguments only one level.
Call concat() on an array with zero or more arrays or values. It creates a new shallow array, spreading array arguments by one level and appending non-array values as individual elements.
concat()
concat(value1)
concat(value1, value2)
concat(value1, value2, /* …, */ valueN)| Name | Type | Description |
|---|---|---|
value1, …, valueN | any | Arrays and/or values to concatenate into a new array. Array arguments are spread by one level unless their spreadability behavior prevents it. |
A new Array instance containing the calling array's elements followed by the concatenated arguments.
const first = [1, 2];
const second = [3, 4];
const combined = first.concat(second, 5);
console.log(combined); // [1, 2, 3, 4, 5]
console.log(first); // [1, 2]Creates a new array from both input arrays and an additional value while leaving the original arrays unchanged.
Combine two or more lists into one without changing the originals.
Add one or more values while producing a separate result.
Duplicate an array shallowly before making changes.
Flatten one level of array-like input when Symbol.isConcatSpreadable is enabled.
Array.prototype.flat()Array.prototype.push()Array.prototype.slice()Symbol.isConcatSpreadableSpread syntax (...)