JS: Array.prototype.concat

By Xah Lee. Date: . Last updated: .

myArray.concat(new1, new2 …)

Returns a new array.

The new array will have arguments added to the end of the array myArray.

Each argument can be any type. If it is a array, it will be flattened 1 level (That is, remove the outmost brackets).

Original array variable is not modified.

// example of using array.concat()
console.log ( [1].concat( 2, [3], 4, {k9:9}) );
// [ 1, 2, 3, 4, { k9: 9 } ]

To add without flatten, use push. See: JS: Array.prototype.push.

For how to flatten nested array, see: JS: Flatten Nested Array.

Reference

ECMAScript® 2016 Language Specification#sec-array.prototype.concat

Array Topic

  1. JS: Array Basics
  2. JS: Understand JS Array
  3. JS: Create Array
  4. JS: Sparse Array
  5. JS: Array-Like Object
  6. JS: Array How-To

  1. JS: Array Object
  2. JS: Array.prototype
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth