unshift()
Array
instance method
JS Home <<
JS Reference <<
Array <<
unshift()
Description
Adds element(s) to the start of an array and returns the new length of the array.
Syntax
Signature | Description |
---|---|
anArray.unshift(element1, ..., elementN) | Adds element(s) to the start of an array and returns the new length of the array. |
Parameters
Parameter | Description |
---|---|
element1, ..., elementN | The element(s) to add to the start of the array. |
Examples
The code below creates an array then adds some elements to the start.
/*
Create an array of names.
Add two elements on to start of array.
someNumbers = [1, 1, 0, 0] after unshift().
Variable arrayLength = 4 after unshift().
*/
var someNumbers = [0, 0];
alert(someNumbers + ' - ' + someNumbers.length);
someNumbers.unshift(1, 1);
alert(someNumbers + ' - ' + someNumbers.length);
Related Tutorials
JavaScript Intermediate Tutorials - Lesson 1 - Arrays