HOME HTML CSS JAVASCRIPT SQL PHP JQUERY XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

JavaScript Array unshift() Method

Array Object Reference JavaScript Array Object

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

Internet Explorer Firefox Opera Google Chrome Safari

The unshift() method is supported in all major browsers.

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


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

Return Value

Type Description
Number The new length of the array

Technical Details

JavaScript Version: 1.2


Array Object Reference JavaScript Array Object

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates

SHARE THIS PAGE