JavaScript Reference
Array.prototype.pop()
Remove and return the last element. Mutates in place.
Last updated: October 2026
Definition: Array.prototype.pop() removes the last element from an array and returns that element. It mutates the original array and reduces its length by one. On an empty array, it returns undefined.
🎬 Live Example
Five pop patterns. Click any button to see the output.
💡 pop is the fastest way to remove from the end — O(1) time.
Basic Syntax
array.pop()
// No arguments
// Returns the removed element, or undefined if empty
// Mutates the original array
Remove the Last Element
const fruits = ['apple', 'banana', 'cherry'];
const last = fruits.pop();
// last: 'cherry'
// fruits: ['apple', 'banana']
Safe on Empty Arrays
const arr = [];
const val = arr.pop();
// val: undefined
// arr: [] (no error)
Good to know: pop never throws, unlike accessing arr[arr.length-1] which can return undefined silently.
Drain an Array (Stack Pattern)
const stack = [1, 2, 3, 4, 5];
while (stack.length > 0) {
process(stack.pop());
}
// Processes 5, 4, 3, 2, 1
// stack is now empty
Undo / History Pattern
const history = [];
history.push('type A');
history.push('type B');
const lastAction = history.pop();
// lastAction: 'type B'
// history: ['type A']
Classic use case — pop is the foundation of undo stacks and LIFO queues.
Non-Destructive Alternative
// ❌ Mutates
const arr = [1, 2, 3];
arr.pop();
// ✅ Non-destructive
const original = [1, 2, 3];
const without = original.slice(0, -1);
// original: [1, 2, 3]
// without: [1, 2]
// ✅ ES2023+ (modern browsers)
const newer = original.toSpliced(-1, 1);
🎯 pop vs shift vs splice vs slice
| Method | Removes from | Complexity |
|---|---|---|
pop | End | O(1) — fastest |
shift | Start | O(n) — re-indexes |
splice | Anywhere | O(n) |
slice | Nowhere (copy) | O(n) — non-destructive |
⚠️ Common Mistakes
- Using pop when you need the array. pop returns the element, not the array.
- Assuming pop throws on empty. It returns undefined silently.
- Popping in a forward loop. Use
while (arr.length)or iterate backwards. - React state mutation.
state.pop()won't trigger re-renders. Usestate.slice(0, -1). - Confusing pop with shift. pop = end, shift = start.
- Expecting pop to accept an index. Use
splicefor arbitrary positions.
❓ Frequently Asked Questions
What does Array.pop() do in JavaScript?
Array.pop() removes the last element from an array and returns that element. It mutates the original array and reduces its length by one.
What does pop return on an empty array?
pop() returns undefined on an empty array and does not change the array. It never throws an error.
What is the difference between pop and shift?
pop removes from the end of the array (fast, O(1)). shift removes from the beginning (slower, O(n)) because it re-indexes every element.
Does pop modify the original array?
Yes. pop() mutates the array by removing the last element. For a non-destructive alternative, use slice(0, -1) or the newer toSpliced() method.
What is the difference between pop and splice?
pop always removes the last element and takes no arguments. splice can remove any element at any position and accepts start and deleteCount arguments.