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.
  • pop() is sometimes written as Array.prototype.pop() because it is a method of the Array object's prototype property.

Syntax of the pop() Method

pop() accepts no argument. Here is the syntax:

callingArray.pop();

Example 1: Remove an Array's Last Item

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: Remove an Array's Last Item

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.

Tweet this article