How to Pop the First Element from an Array in JavaScript?

Estimated read time 1 min read

To remove the first element from a JavaScript array, you can use the shift() method. This method removes the first element of the array and returns it. Here’s an example:

let arr = [1, 2, 3, 4];
let firstElement = arr.shift();

console.log(firstElement); // outputs 1
console.log(arr); // outputs [2, 3, 4]

In this example, shift() is called on the arr array, and the first element (1) is removed and stored in the firstElement variable. The resulting array is [2, 3, 4].

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply