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