pop() in JavaScript – How to Remove Last Array Element
Whenever you use pop() on an array, the method does the following:
- It removes its calling array's last item.
- It returns the removed item.
note
- A calling array is an array on which you used
pop()
. So, inbestColorsList.pop()
,bestColorsList
is the calling array. pop()
is sometimes written asArray.prototype.pop()
because it is a method of theArray
object'sprototype
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]
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"]
The snippet above used pop()
to pop out fruitsArray
's last item.
note
Overview
This article discussed what JavaScript's pop()
method does. We also used examples to see how it works.