Add an item at the beginning of the array in JavaScript

September 30, 2014

To add an item at the beginning of the array use unshift (it returns the length of the new array):

js
var a = [1, 2];
a.unshift(3); => a: [3, 1, 2];
a.unshift(4, 5); => a: [4, 5, 3, 1, 2];