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];
const yy = xx.unshift(7, [8]);
console.log(yy === 3);
console.log(JSON.stringify(xx) === "[7,[8],3]");
// example of unshift
const xx = [3, 4];
const yy = xx.unshift(7, [8]);

console.log(yy);
// 4

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

JavaScript. Array Push Pop