The concat()
method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
var new_array = old_array.concat([value1[, value2[, ...[, valueN]]]])
valueN
Optional
valueN
is undefined
, concat
returns a shallow copy of the existing array on which it is called. See the description below for more details.A new Array
instance.
The concat
method creates a new array consisting of the elements in the object on which it is called, followed in order by, for each argument, the elements of that argument (if the argument is an array) or the argument itself (if the argument is not an array). It does not recurse into nested array arguments.
The concat
method does not alter this
or any of the arrays provided as arguments but instead returns a shallow copy that contains copies of the same elements combined from the original arrays. Elements of the original arrays are copied into the new array as follows:
concat
copies object references into the new array. Both the original and new array refer to the same object. That is, if a referenced object is modified, the changes are visible to both the new and original arrays. This includes elements of array arguments that are also arrays.String
, Number
, and Boolean
objects): concat
copies the values of strings and numbers into the new array.Note: Concatenating array(s)/value(s) will leave the originals untouched. Furthermore, any operation on the new array (except operations on elements which are object references) will have no effect on the original arrays, and vice versa.
The following code concatenates two arrays:
var alpha = ['a', 'b', 'c']; var numeric = [1, 2, 3]; alpha.concat(numeric); // result in ['a', 'b', 'c', 1, 2, 3]
The following code concatenates three arrays:
var num1 = [1, 2, 3], num2 = [4, 5, 6], num3 = [7, 8, 9]; var nums = num1.concat(num2, num3); console.log(nums); // results in [1, 2, 3, 4, 5, 6, 7, 8, 9]
The following code concatenates three values to an array:
var alpha = ['a', 'b', 'c']; var alphaNumeric = alpha.concat(1, [2, 3]); console.log(alphaNumeric); // results in ['a', 'b', 'c', 1, 2, 3]
The following code concatenates nested arrays and demonstrates retention of references:
var num1 = [[1]]; var num2 = [2, [3]]; var nums = num1.concat(num2); console.log(nums); // results in [[1], 2, [3]] // modify the first element of num1 num1[0].push(4); console.log(nums); // results in [[1, 4], 2, [3]]
Specification | Status | Comment |
---|---|---|
ECMAScript 3rd Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.2. |
ECMAScript 5.1 (ECMA-262) The definition of 'Array.prototype.concat' in that specification. | Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Array.prototype.concat' in that specification. | Standard | |
ECMAScript Latest Draft (ECMA-262) The definition of 'Array.prototype.concat' in that specification. | Draft |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 1 | Yes | 1 | 5.5 | Yes | Yes |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | Yes | Yes | 4 | Yes | Yes | Yes |
Server | |
---|---|
Node.js | |
Basic support | Yes |
push
/ pop
— add/remove elements from the end of the arrayunshift
/ shift
— add/remove elements from the beginning of the arraysplice
— add/remove elements from the specified location of the arrayString.prototype.concat()
Symbol.isConcatSpreadable
– control flattening.
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat