Javascript - Array unshift() Method

The unshift() method adds new items to the beginning of an array, and returns the new length.

Description

The unshift() method adds new items to the beginning of an array, and returns the new length.

This method changes the length of an array.

You can add new items at the end of an array, use the push() method.

Syntax

array.unshift(item1,item2, ...,itemX)

Parameter Values

Parameter Require Description
item1, item2, ..., itemXRequired. The item(s) to add to the beginning of the array

Return

A Number, representing the new length of the array

Example

Add new items to the beginning of an array:

Demo

var myArray = ["XML", "Json", "Database", "Mango"];
console.log( myArray );//from   ww  w. ja va2 s  .  c o  m

myArray.unshift("Lemon", "PineDatabase");
console.log( myArray );

Result