Trusted answers to developer questions

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

Get Started With Data Science

Learn the fundamentals of Data Science with this free course. Future-proof your career by adding Data Science skills to your toolkit — or prepare to land a job in AI, Machine Learning, or Data Analysis.

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);

RELATED TAGS

array
shift()
method
what
javascript
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?