How to Manipulate Arrays With shift() in JavaScript

Posted by

The shift() method in JavaScript is used to remove the first element from an array and return that element. This method modifies the original array and updates the index of all remaining elements.

Here is an example of how the shift() method can be used:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let fruits = ["apple", "banana", "cherry"];
let firstFruit = fruits.shift();
console.log(firstFruit); // Output: "apple"
console.log(fruits); // Output: Array ["banana", "cherry"]

[/dm_code_snippet]

You can see that the first element “apple” is removed and returned, and the remaining elements are updated to have their index reduced by 1.

You can also use shift in a while loop to remove all elements from an array,

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

while (fruits.length > 0) {
  fruits.shift();
}
console.log(fruits);  // Output: []

[/dm_code_snippet]

shift() is useful when you need to remove the first element of an array and also want to know what element has been removed. It’s the counterpart of the pop() method which removes the last element of an array. Note that shift() and pop() have O(n) time complexity due to them reindexing the elements internally.

It’s important to note that the shift() method only works on the first element of an array, to remove an element from other position splice() method should be used.

In addition to removing the first element of an array, the shift() method also returns the removed element, allowing you to save it in a variable or use it in other operations. You can use the return value to chain it to other operations like so:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let fruits = ["apple", "banana", "cherry"];
let firstFruit = fruits.shift();
let message = "The first fruit is " + firstFruit + ".";
console.log(message); // Output: "The first fruit is apple."

[/dm_code_snippet]

The shift() method also updates the length of the array, as well as the index of all remaining elements. This means that if you have any variables or other elements that reference a specific index in the array, you will need to update them accordingly after using the shift() method.

It is important to note that the shift() method modifies the original array, if you want to keep the original array intact, use slice() method or the spread operator (…) with the index to get a new array.

It’s worth mentioning that shift() method is not supported by all browsers and older version of them, so you may want to check the browser compatibility before using it in production.

In conclusion, the shift() method can be a useful tool when working with arrays in JavaScript, allowing you to remove the first element of an array and retrieve its value, and reordering the remaining elements. It’s a common alternative for removing elements from beginning of array.