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.
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.