How to use Javascript array unshift() method

Description

unshift() adds items to the front of an array and returns the new array length.

Example


var colors = new Array(); //create an array 
        //from ww  w  . j a  v  a  2 s.c o  m
var count = colors.unshift("A", "B"); //push two items 
console.log(count); //2 
count = colors.unshift("C"); //push another item on 
        
console.log(count); //3 

var item = colors.pop(); //get the first item 
console.log(item); //"B" 
console.log(colors.length); //2 

The code above generates the following result.





















Home »
  Javascript »
    Javascript Reference »




Array
Canvas Context
CSSStyleDeclaration
CSSStyleSheet
Date
Document
Event
Global
History
HTMLElement
Input Element
Location
Math
Number
String
Window