Latest web development tutorials
 

JavaScript Array unshift() Method

< JavaScript Array Reference

Example

Add new items to the beginning of an array:

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

The result of fruits will be:

Lemon,Pineapple,Banana,Orange,Apple,Mango
Try it Yourself »

Definition and Usage

The unshift() method adds new items to the beginning of an array, and returns the new length.

Note: This method changes the length of an array.

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


Browser Support

The numbers in the table specify the first browser version that fully supports the method.

Method
unshift() 1.0 5.5 1.0 Yes Yes

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


Syntax

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

Parameter Values

Parameter Description
item1,item2, ..., itemX Required. The item(s) to add to the beginning of the array

Technical Details

Return Value: A Number, representing the new length of the array
JavaScript Version: 1.2

< JavaScript Array Reference