JS: Array.prototype.unshift

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

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

// new length
console.log(yy);
// 3
// add a array item
const xx = [3, 4];
xx.unshift([1,2]);
console.log(xx);
// [ [ 1, 2 ], 3, 4 ]