JS: Array.prototype.concat

By Xah Lee. Date: . Last updated: .
arrayX.concat(new1, new2, etc)
Return a new array by joining arguments to the array. Each argument can be any type. If it is a array, it will be flattened 1 level (That is, remove the outmost brackets).
// example of using array.concat()

const xx = [1];

console.log(xx.concat(2));
// [ 1, 2 ]

// add a object
console.log(xx.concat({ k9: 9 }));
// [ 1, { k9: 9 } ]

// var unchanged
console.log(xx);
// [1]
// adding an array flattens it 1 level
const xx = [1];
console.log(xx.concat([2, [3]]));
// [ 1, 2, [ 3 ] ]

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

For how to flatten nested array, see: Array.prototype.flat .

JavaScript, Array, add/remove items

BUY ΣJS JavaScript in Depth