JS: Array.prototype.concat

By Xah Lee. Date: . Last updated: .
xArray.concat(new1, new2, etc)
  • add items to the end of array.
  • Return a new array.
  • Each argument can be any type.
  • If an argument is a array, it is flattened 1 level.
const xx = [1];
console.log(JSON.stringify(xx.concat(2)) === "[1,2]");
// true

// original unchanged
console.log(JSON.stringify(xx) === "[1]");
// true
// if arg is an array, it is flattened 1 level
console.log(JSON.stringify([1].concat([2, [3]])) === "[1,2,[3]]");
// true

JavaScript. Add or Remove Subarray