Array.prototype.concat
Merges arrays into a new array.
Available since: ES3
Syntax
array.concat(value1, value2, /* … */)Parameters
| Parameter | Type | Description |
|---|---|---|
| value1, … | any | Arrays or single values to append. |
Return value
ArrayA new array.
Examples
Merging two arrays:
const a = [1, 2];
const b = [3, 4];
console.log(a.concat(b));Output:
[
1,
2,
3,
4
]The same with spread — more common today:
const a = [1, 2];
const b = [3, 4];
console.log([...a, ...b, 5].join(', '));
console.log(a.join(', '));Output:
1, 2, 3, 4, 5
1, 2Notes
The original arrays are not changed. Nested arrays are unpacked only one level deep.
See also
- Spread syntax — Three dots spread an array or object into separate items.
- Array.prototype.push — Appends elements to the end of an array.
- Array.prototype.flat — Flattens nested arrays into one.
Related recipes
In the tutorial
- Array methods — How to add and remove array elements in JavaScript: push, pop, shift, unshift, slice, splice, concat and join.