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

Description

Removes the first element of an array and returns that elements value to the caller.

Syntax

Signature Description
anArray.shift()Removes the first element of an array and returns that elements value to the caller.

Parameters

None.

Examples

The code below creates an array then removes the first element.


/*
 Create an array of days of the week.
 Pop the last element.
 Variable firstDayOfWeek = 'Mon'.
 */

var week = ['Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat', 'Sun'];
var firstDayOfWeek = week.shift();
alert (week + ' - ' + firstDayOfWeek);

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