Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript unshift() Method

JavaScript unshift() Method

Array Object Reference JavaScript Array object

Example

Adds new items to the beginning of an array:

var fruits = ["Banana", "Orange", "Apple", "Mango"];

fruits.unshift("Lemon","Pineapple");

fruits Will output:

Lemon,Pineapple,Banana,Orange,Apple,Mango

Try it »


Definition and Usage

The unshift() method adds one or more elements to the beginning of an array and returns the new length.

Note: This method changes the length of the array.

Tip: To add new items to the end of an array, use push() method.


Browser support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the unshift() method; Internet Explorer 8 and earlier IE versions do not support it.

Note: In Internet Explorer 8 and earlier IE versions, the unshift() method returnsundefined.


Syntax

array

.unshift(

item1

,

item2

, ...,

itemX

)

Parameter Values

Parameters Description
item1,item2, ..., itemX Optional. One or more elements to add to the beginning of the array.

Return Value

Type Description Number The new length of the array

Technical Details

JavaScript version: 1.2

Array Object Reference JavaScript Array object