JavaScript Array unshift() method
- Previous Page toString()
- Next Page values()
- Go to the Previous Level JavaScript Array Reference Manual
Definition and usage
unshift()
The method adds a new item to the beginning of the array and returns the new length.
Note:unshift()
The method changes the length of the array.
Tip:To add a new item to the end of the array, use push()
Method.
Example
Add a new item to the beginning of the array:
var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.unshift("Lemon","Pineapple");
Syntax
array.unshift(item1, item2, ... , itemX)
Parameter value
Parameter | Description |
---|---|
item1, item2, ... , itemX | Required. The item to be added to the beginning of the array. |
Technical details
Return value: | A number, representing the new length of the array. |
---|---|
JavaScript version: | ECMAScript 1 |
Browser support
All browsers fully support unshift()
Method:
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Chrome | IE | Edge | Firefox | Safari | Opera |
Support | Support | Support | Support | Support | Support |
Note:In Internet Explorer 8 and earlier versions, the unshift() method returns undefined.
Related pages
Tutorial:JavaScript array
Tutorial:JavaScript array Const
Tutorial:JavaScript array methods
Tutorial:JavaScript sort array
Tutorial:JavaScript array iteration
Manual:JavaScript Array.push() method
- Previous Page toString()
- Next Page values()
- Go to the Previous Level JavaScript Array Reference Manual