JavaScript arrays JavaScript arrays arrays arrays

JavaScript arrays


W3CSchools is really outdated, the unshift method is part of the ECMAScript 3rd Edition standard which was approved and published as of December 1999.

There is no reason to avoid it nowadays, it is supported from IE 5.5 up.

It is safe to use it, even modern libraries like jQuery or MooTools internally use it (you can check the source code :).

var array = [2,3];array.unshift(1); // returns 3// modifies array to [1, 2, 3]

This method returns the new array length, and inserts the element passed as argument at the first position of the array.


Using splice method.

array.splice(index,howmany,element1,.....,elementX);

For inserting, 'howmany' = 0


You can try this:

var newItem = 3;arr = [newItem].concat(arr);

Another option is to use push and reverse the indices - you can easily write an object that does that.