Shifting is akin to popping, but it operates on the first element instead of the last.
The shift() method eliminates the first element of the array and shifts all subsequent elements to a lower index.
Example
const fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; fruits.shift(); |
The shift() method returns the value that was removed from the array’s beginning.
Example
const fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; let fruit = fruits.shift(); |
The unshift() method inserts a new element at the beginning of an array and shifts existing elements to higher indices.
Example
const fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; fruits.unshift(“Lemon”); |
The unshift() method returns the updated length of the array.
Example
const fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; fruits.unshift(“Lemon”); |