Skip to main content

pop() in JavaScript – How to Remove Last Array Element

Whenever you use pop() on an array, the method does the following:

  1. It removes its calling array's last item.
  2. It returns the removed item.
note

A calling array is an array on which you used pop(). So, in bestColorsList.pop(), bestColorsList is the calling array.

Syntax of the pop() Method

callingArray.pop()

As shown in the snippet above, pop() does not accept any argument.

pop() Example 1

const numbersArray = [1, 2, 3, 4];

console.log(numbersArray.pop());

// The invocation above will return:
4

// Check the numbersArray's current content:
console.log(numbersArray);

// The invocation above will return:
[1, 2, 3]

Try it on StackBlitz

You can see that pop() removed and returned numbersArray's last item (4).

Also, note that pop() changed the original array's length.

Example 2

const fruitsArray = ["Mango", "Apple", "Orange"];
const removedFruit = fruitsArray.pop();

console.log(removedFruit); // returns "Orange"
console.log(fruitsArray); // returns ["Mango", "Apple"]

Try it on StackBlitz

The snippet above used pop() to pop out fruitsArray's last item.

note
  • To remove an array's first item, use shift().
  • To remove an item at a specific index position, use the splice() method.

Overview

This article discussed what JavaScript's pop() method does. We also used examples to see how it works.

If you like this article, please Tweet it.

Thanks for reading!