What is the array.shift() method in JavaScript?

array.shift() is a method that removes the first element from an​ array and shifts the array to the left by one position.

The following slides illustrate how the method works:

1 of 5

array.shift() can be called on by any type of array. This method is very useful when the removal of the first element is required because it not only removes the first element; ​it also preserves the order of the rest of the array.

Implementation

The following code uses array.shift() ​​and shifts the array twice:

var fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log(fruits);
fruits.shift();
console.log(fruits);
fruits.shift();
console.log(fruits);

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved