Visit Mozilla.org

Core JavaScript 1.5 Reference:Global Objects:Array:unshift

From MDC


Contents

[edit] Summary

Adds one or more elements to the beginning of an array and returns the new length of the array.

Method of Array
Implemented in: JavaScript 1.2, NES 3.0
ECMA Version: ECMA-262 Edition 3

[edit] Syntax

arrayName.unshift(element1, ..., elementN)

[edit] Parameters

element1, ..., elementN 
The elements to add to the front of the array.

[edit] Examples

[edit] Example: Adding elements to an array

The following code displays the myFish array before and after adding elements to it.

myFish = ["angel", "clown"];
document.writeln("myFish before: " + myFish);
unshifted = myFish.unshift("drum", "lion");
document.writeln("myFish after: " + myFish);
document.writeln("New length: " + unshifted);

This example displays the following:

myFish before: ["angel", "clown"]
myFish after: ["drum", "lion", "angel", "clown"]
New length: 4

[edit] See Also

pop, push, shift