JavaScript(JS) array method - shift

The shift() method in JavaScript is used to remove the first element from an array and returns that element. It modifies the original array.

Here's an example of how to use the shift() method:

re‮ef‬r to:theitroad.com
let array = ["one", "two", "three", "four"];
let firstElement = array.shift();
console.log(firstElement); // "one"
console.log(array); // ["two", "three", "four"]

In this example, we have an array array containing the elements ["one", "two", "three", "four"]. We use the shift() method to remove the first element from the array and return it.

The shift() method modifies the original array and returns the removed element. After the shift() method is executed, the array is now ["two", "three", "four"].

We store the removed first element in the variable firstElement, and log it to the console using console.log(firstElement). We also log the modified array to the console using console.log(array).