JavaScript unshift()Methods
Examples
Add new items to the beginning of an array:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.unshift("Lemon","Pineapple");
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
![]()
![]()
![]()
![]()
![]()
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
Technical Details
| JavaScript Version: | 1.2 |
|---|
Other Extensions
JavaScript Array Object