JavaScript unshift()Methods

Array 对象参考手册JavaScript Array Object

Examples

Add new items to the beginning of an array:

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.unshift("Lemon","Pineapple");

fruitsWill output:

Lemon,Pineapple,Banana,Orange,Apple,Mango

Try it Yourself »

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 will change the number of elements in the array.

Tip:To add new items to the end of an array, use thepush()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:The unshift() method returns in Internet Explorer 8 and earlier IE versionsundefined。


Syntax

array.unshift(item1,item2, ..., itemX)

Parameter Values

Parameter Description
item1,item2, ..., itemX Optional. Adds one or more elements to the beginning of the array.

Return Value

Type Description Number The new length of the array

Technical Details

JavaScript Version: 1.2


Array 对象参考手册JavaScript Array Object

Other Extensions