//The shift() method removes the first element from an array //and returns that removed element. //This method changes the length of the array. const array1 = [1, 2, 3]; const firstElement = array1.shift(); console.log(array1); // expected output: Array [2, 3] console.log(firstElement); // expected output: 1
Here is what the above code is Doing:
1. The first line creates an array with three elements.
2. The second line calls the shift() method on the array.
3. The shift() method removes the first element (1) from the array and returns it.
4. The third line logs the array to the console.
5. The fourth line logs the first element to the console.