Open In App

What is the use of the Array.shift() method in JavaScript?

Last Updated : 15 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The Array.shift() method in JavaScript is used to remove the first element from an array and return that removed element. This method modifies the original array by removing the first element and shifting all subsequent elements to a lower index.

Syntax:

array.shift()

Example: Here, the shift() method is called on the array containing [1, 2, 3, 4, 5]. It removes the first element (1) from the array, shifting all subsequent elements to a lower index. The modified array becomes [2, 3, 4, 5], and the removed element (1) is returned.

Javascript




const array = [1, 2, 3, 4, 5];
// Removes the first element (1)
const removedElement = array.shift();
console.log(array);
console.log(removedElement);


Output

[ 2, 3, 4, 5 ]
1


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads