unshift()  Array  instance method setterS2C Home  « Globals « 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, ..., elementNThe 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);

Press the button below to action the above code:


Related Tutorials

JavaScript Intermediate Tutorials - Lesson 1 - Arrays

go to home page Homepage go to top of page Top