English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
unshift()This method adds one or more elements to the beginning of an array and returns the new length of the array.
Note:To append a new element to the end of an array, please usepush()Method.
array.unshift(element1..., elementN)
var fruits = ["Banana", "Mango", "Apple"]; fruits.unshift("Strawberry");Test and see‹/›
The numbers in the table specify the first browser version that fully supports the unshift() method:
Method | |||||
unshift() | 1 | 1 | Yes | Yes | 9 |
Parameter | Description |
---|---|
elementN | Element to be added to the beginning of the array |
Return Value: | Return the new length of the array |
---|---|
JavaScript Version: | ECMAScript 1 |
The following code adds three elements to the array. The total variable contains the new length of the array:
var fruits = ["Banana", "Mango", "Apple"]; var total = fruits.unshift("Strawberry", "Lychee", "Guava");Test and see‹/›
The following code adds the value of the input field to the array:
Banana, Mango, Apple