X
HOME HTML5 CSS3 JAVASCRIPT SQL PHP5 JQUERY ANGULAR BOOTSTRAP3 XML MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

JavaScript Array unshift() Method

JavaScript Array Reference 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 JavaScript Array Reference

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB HOSTING

UK Reseller Hosting

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates

SHARE THIS PAGE

facebook