Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › JavaScript › Reference › pop()

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.

Input:

                
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
1

Remove the Last Element

const fruits = ['apple', 'banana', 'cherry'];
const last = fruits.pop();
// last:   'cherry'
// fruits: ['apple', 'banana']
2

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.

3

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
4

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.

5

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
popEndO(1) — fastest
shiftStartO(n) — re-indexes
spliceAnywhereO(n)
sliceNowhere (copy)O(n) — non-destructive

⚠️ Common Mistakes

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

🎯 What's Next?

← All JavaScript Reference