Javascript - Array shift() Method

The shift() method removes the first item from an array.

Description

The shift() method removes the first item from an array.

It changes the length of the array and returns the removed item.

You can remove the last item of an array, use the pop() method.

Syntax

array.shift()

Parameters

None

Return Value

Any type, representing the removed array item.

An array item can be of type string, number, array, boolean, or any other object types.

Example

Remove the first item of an array:

Demo

var myArray = ["XML", "Json", "Database", "Mango"];
console.log( myArray );//from   w w  w.  j  a v a2s  . com

myArray.shift();
console.log( myArray );

var x = myArray.shift();
console.log( myArray );
console.log(x);

Result