Array Shift method in Javascript

The array shift of the Javascript method removes the first element from an array. It will return the element it removes to the caller. This article will look at JavaScript’s shift method, Its syntax, how it works, and how to use it.

Array Shift Method

The array shift method removes and returns the first element from the array. If the array is empty, it will do nothing but return undefined.

It mutates the array and changes its length.

The shift method returns the element it removed from the array. If it does not remove the element, it will return undefined.

Note that if the shift method returns undefined does not mean that the array is empty. It may happen the element it removed has undefined in it, or it may be an empty slot(sparse array)

Syntax

The syntax of the array.shift method is as follows:

It does not take any arguments.

Array shift Example

In the following example, we have an array of numbers. We call the shift method on it. It removes the first element, number 1, from the array and returns its value.

We store the returned value in a variable named removedValue. Finally, we log the modified array and the removed value to the console.

Array Shift Performance

Shift removes the first element and re-indexes all the other elements in the array. Re-indexing is a time-consuming process, especially if the array is large. If you call shift frequently, it may be wiser to rethink the structure of your application.

Vs. Pop

Array shift & pop are similar, except that shift removes the element from the start, while array pop removes the element from the end.

The code below uses the pop method to remove the last element from the array.

Vs. Slice

The slice method is another option to remove the first element. It will not mutate the original array but returns a new array.

Hence if you do not want to mutate the array, using the slice may be an option. But note that the slice method is slower as it needs to create a new array and update each element with the values from the original array.

Vs splice()

The splice is another way to remove the last element from the array. The syntax of the splice is as follows.

Where start is the index position from where you want to remove the elements, and deleteCount is the number of elements you want to remove from the array. The subsequent arguments, if any, are inserted at the start index.

We can remove the last element from the array by setting start as 0 and deleteCount as 1.

Splice, just like pop, mutates the original array.

Array Like objects and shift method

The shift method is generic. It only expects this value to have a length property and integer-keyed properties.

Summary

  • The array shift method removes and returns the first element from the array.
  • If the array is empty, it will do nothing but return undefined.
  • It mutates the array and changes its length.

Reference

Array.shift

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top