Latest web development tutorials

JavaScript unshift () method

Array Object Reference JavaScript Array Object

Examples

Add a new item to the beginning of the 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

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

Note: This method will change the number of the array.

Tip: add a new item to the end of the array, use the push () method.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

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

Note: unshift () method in Internet Explorer 8 and earlier versions of IE returns undefined.


grammar

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

Parameter Values

参数 描述
item1 , item2 , ..., itemX 可选。向数组起始位置添加一个或者多个元素。

return value

Type description Number The new length of the array

technical details

JavaScript version: 1.2


Array Object Reference JavaScript Array Object