Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Array.prototype.concat

Merges arrays into a new array.

Available since: ES3

Syntax

array.concat(value1, value2, /* … */)

Parameters

ParameterTypeDescription
value1, …anyArrays 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, 2

Notes

The original arrays are not changed. Nested arrays are unpacked only one level deep.

See also

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.

Thanks for reading!

Donate to the site — we work hard for you.