Array.prototype.pop()

The natural partner to push. Unlike most removals it is O(1), because nothing after it needs to shift.

Array methodES3 (1999)Live demo
Common call
const last = items.pop()
Returns
the removed element, or undefined
Replaces
reading items[items.length - 1] then shortening the array
Watch out
an empty array gives undefined, indistinguishable from a real undefined element
array.pop()
→ any

Demo

Live evaluation
Try:
Inputs
itemsnumber[]numbers, comma separated
Output
[1, 2, 3].pop()
3

The output is the element that was REMOVED, not the shortened array — the array itself is modified in place and is not what comes back. On an empty array you get undefined rather than an error, which is convenient in a while loop but means you cannot tell "the array was empty" from "the last element happened to be undefined".

Common patterns

Stack behaviour
push and pop together give LIFO.
stack.push(x);
const top = stack.pop();
Drain an array
The truthiness check guards the empty case.
while (items.length) {
  handle(items.pop());
}
Peek without removing
at(-1) reads the last element and leaves it alone.
const last = items.at(-1);

Examples

1. Returns the element
[1, 2, 3].pop()
Returns
3
2. The array after
const a = [1, 2]; a.pop(); a
Returns
[1]
3. Empty is undefined
[].pop()
Returns
undefined
4. Length drops
const a = [1, 2]; a.pop(); a.length
Returns
1
5. Peek instead
[1, 2, 3].at(-1)
Returns
3 // not removed
6. Not the array
const a = [1, 2]; const r = a.pop(); r
Returns
2 // not [1]

Pitfalls

1. Empty gives undefined, not an error
Popping an empty array is silent. A loop that pops without checking the length runs forever on the undefined, or processes a phantom element, rather than failing loudly.
Phantom element
while (true) handle(items.pop());
handles undefined forever
Check the length
while (items.length) handle(items.pop());
stops cleanly
2. It returns the element, not the array
Same shape of mistake as push. Assigning the result gives the removed value; the shortened array is the original variable.
Got the element
const rest = items.pop();
the last element
The array is the original
items.pop();
const rest = items;
the shortened array
3. It mutates a shared array
Popping a prop or a piece of state removes the element for every holder, and leaves the reference unchanged so identity comparisons see nothing.
Mutates state
state.items.pop();
same reference, no re-render
Copy instead
setItems(state.items.slice(0, -1));
new array

When to use

Use it
  • Stack behaviour with push
  • Draining an array from the end
  • Removing the last element in place
Reach for something else
  • You only want to READ the last element → at(-1)
  • The array is shared or state → slice(0, -1)
  • You need FIFO order → shift, or a proper queue

Notes

Complexity
O(1) — nothing after the last element needs to move
Return
The removed element or undefined; the array is modified in place
CPython impl
V8: Builtins-array-pop.tq
Memory
May shrink the backing store
Thread-safe
Single-threaded; mutating a shared array is the hazard here

FAQ

at(-1) on modern runtimes, or arr[arr.length - 1] anywhere. Both leave the array alone, which pop never does.

const last = items.at(-1);

History

ES3
pop standardised in 1999 alongside push, shift and unshift.