English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript Array unshift() Method

 JavaScript Array Object

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.

Syntax:

array.unshift(element1..., elementN)
var fruits = ["Banana", "Mango", "Apple"];
fruits.unshift("Strawberry");
Test and see‹/›

Browser Compatibility

The numbers in the table specify the first browser version that fully supports the unshift() method:

Method
unshift()11YesYes9

Parameter Value

ParameterDescription
elementNElement to be added to the beginning of the array

Technical Details

Return Value:Return the new length of the array
JavaScript Version:ECMAScript 1

More Examples

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

 JavaScript Array Object