JS: Array.prototype.push

By Xah Lee. Date: . Last updated:.
xArray.push(new1, new2, etc)
  • Add items to the end of the array.
  • Return the new length.
// push one item
const xx = [3];
const yy = xx.push(7);

console.log(xx);
// [ 3, 7 ]

// result is new length
console.log(yy);
// 2
// push multiple items
const xx = [3];
xx.push(7,8,9);
console.log(xx);
// [ 3, 7, 8, 9 ]
// push a array as item

const xx = [3];
const yy = xx.push([7]);
console.log(xx);
// [ 3, [ 7 ] ]

// note, it does not get flattened