unshift()  Array  instance method JS Home  <<  JS Reference  <<  Array  <<  unshift()

Description

Adds element(s) to the start of an array and returns the new length of the array.

Syntax

Signature Description
anArray.unshift(element1, ..., elementN)Adds element(s) to the start of an array and returns the new length of the array.

Parameters

Parameter Description
element1, ..., elementNThe element(s) to add to the start of the array.

Examples

The code below creates an array then adds some elements to the start.


/*
 Create an array of names.
 Add two elements on to start of array.
 someNumbers = [1, 1, 0, 0] after unshift().
 Variable arrayLength = 4 after unshift().
 */

 var someNumbers = [0, 0];
alert(someNumbers + ' - ' + someNumbers.length);
someNumbers.unshift(1, 1);
alert(someNumbers + ' - ' + someNumbers.length);

Press the button below to action the above code:

Related Tutorials

JavaScript Intermediate Tutorials - Lesson 1 - Arrays

JavaScript Basics

JavaScript Basics

JavaScript Intermediate

JavaScript Intermediate

JavaScript Advanced

JavaScript Advanced

JavaScript Reference

JavaScript Entities

Globals

Array

Class Properties
prototype
Instance Properties
constructor
length
Class Methods
None.
Instance Methods Getters
concat()
join()
slice()
toString()
Instance Methods Setters
pop()
push()
reverse()
shift()
sort()
splice()
unshift()

Statements

Operators