Array Pop Method in JavaScript

The array pop method allows us to remove the last element from an Javascript array. It will return the element it removes to the caller. This article will look at JavaScript‘s “pop()” method with examples. We also look at some of the alternatives to the pop method.

Array pop

The pop method removes and returns the array’s last element. If the array is empty, it will do nothing but return undefined.

It mutates the array and changes its length.

The pop method returns the element it removed from the array. If it does not remove any element or if the array is empty, then it returns undefined.

Note that if the pop 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)

The pop method is generic. We can use it against array-like objects.

Syntax

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

Array pop Example

In the following code. we create an array of numbers and then call the pop() method on it. The method removes the last element from the array, the number 5, and returns its value. We store the returned value in a variable named lastNumber. Finally, we log the modified array and the removed value to the console.

Vs. Shift

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

The code below calls the shift method on numbers array. As you can see it will remove the first element and of the array and returns it.

Vs. Slice

The slice method can remove the last element and return the new array.

The slice method is much slower than the pop method, as the pop method only requires removing the last element. The slice method needs to create a new array and update each element with the values from the original array.

The advantage of the slice method is that it does not alter the original array.

The slice method returns the new array, while pop returns the element it removes.

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 at start index.

By setting start as numbers.length - 1 and deleteCount as 1, we can remove the last element from the array.

splice just like pop mutates the original array.

The splice method may be faster than the pop method when you want to remove multiple elements from the end of the array.

Array Like objects and pop method

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

In the following code arrayLike object contains properties with integer property names and a length property. We can invoke the call function in JavaScript.

If the object is not an array-like object, the pop method does not throw any errors. But adds the length property with the value zero.

Summary

  • The pop method removes and returns the array’s last element. If the array is empty, it will do nothing but return undefined.
  • It mutates the array and changes its length.
  • The splice is another alternative to remove the last element from the array.

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