JavaScript Array pop() Method

Beginner
⏱️ 7 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Mutates

What You’ll Learn

The pop() method removes the last element from an array, returns that value, and shortens the array by one. This tutorial covers syntax, five examples, empty-array behavior, stack patterns with push(), and how pop() differs from non-mutating methods like slice().

01

Syntax

arr.pop()

02

Last out

End removal

03

Returns

Removed item

04

Empty

undefined

05

Stack

LIFO + push

06

ES3

Universal

Introduction

Arrays often grow and shrink. When you need to take something off the end—the most recently added item, the last breadcrumb, or the top of a stack—pop() is the built-in way to do it in one step.

Unlike map() or slice(), pop() mutates the original array. Always remember that the source array will be shorter after the call.

Understanding the pop() Method

array.pop() removes the element at index length - 1, decrements length, and returns the removed value. If the array is empty, it returns undefined and leaves the array unchanged.

Pair it with push() for a Last-In-First-Out (LIFO) stack: items go on top with push, come off top with pop.

💡
Beginner Tip

To read the last element without removing it, use arr[arr.length - 1] or arr.at(-1). Use pop() only when you want to remove it.

📝 Syntax

General form of Array.prototype.pop:

JavaScript
array.pop()

Parameters

  • None.

Return value

  • The removed last element.
  • undefined if the array is empty.

Side effects

  • Mutates the array: length decreases by 1.
  • Former last index becomes a hole that is discarded (array compacts).

Common patterns

  • const last = arr.pop() — remove and capture.
  • while (stack.length) stack.pop() — drain stack.
  • if (arr.length) arr.pop() — guard empty arrays.
  • stack.push(x); stack.pop() — LIFO pair.

⚡ Quick Reference

GoalCode
Remove last elementarr.pop()
Return valueRemoved element or undefined
Read last without removingarr.at(-1)
Remove first elementarr.shift()
Mutates array?Yes

📋 pop() vs push() vs shift() vs slice()

Choose based on end vs start removal and whether you need to mutate.

pop
remove last

Mutates

push
add last

Stack pair

shift
remove first

Queue end

slice
copy portion

No mutation

Examples Gallery

Open DevTools Console (F12) or use Try-it labs. Example N maps to ?tryit=N.

📚 Getting Started

Remove the last item and inspect results.

Example 1 — Remove the Last Element

Pop "yellow" off a color list and see the shorter array.

JavaScript
const colors = ["red", "green", "blue", "yellow"];

const removed = colors.pop();

console.log(removed);
// "yellow"

console.log(colors);
// ["red", "green", "blue"]
Try It Yourself

How It Works

pop() returns the removed value and updates colors in place. Length drops from 4 to 3.

Example 2 — Empty Array Returns undefined

Calling pop() on an empty array is safe but returns nothing useful.

JavaScript
const empty = [];

const result = empty.pop();

console.log(result);
// undefined

console.log(empty.length);
// 0
Try It Yourself

How It Works

No error is thrown. Check length or compare to undefined carefully if undefined could also be a stored value.

📈 Practical Patterns

Guards, stacks, and non-mutating alternatives.

Example 3 — Guard with length Before Popping

Avoid pointless pops when the array might already be empty.

JavaScript
const colors = ["red"];

let removed;

if (colors.length > 0) {
  removed = colors.pop();
  console.log("Removed:", removed);
} else {
  console.log("Array is empty.");
}
// Removed: red

console.log(colors);
// []
Try It Yourself

How It Works

Checking length first makes intent clear in undo/history features where empty state matters.

Example 4 — Stack with push() and pop()

Classic LIFO: last pushed is first popped.

JavaScript
const stack = [];

stack.push("first");
stack.push("second");
stack.push("third");

console.log(stack.pop());
// "third"

console.log(stack.pop());
// "second"

console.log(stack);
// ["first"]
Try It Yourself

How It Works

Undo stacks, browser history, and DFS algorithms often use this push/pop pattern on arrays.

Example 5 — pop() Mutates; slice() Does Not

Need a copy without changing the original? Use slice, not pop.

JavaScript
const nums = [10, 20, 30];

const copyAllButLast = nums.slice(0, -1);

console.log("slice copy:", copyAllButLast);
// [10, 20]

console.log("original after slice:", nums);
// [10, 20, 30] — unchanged

const popped = nums.pop();

console.log("popped:", popped);
// 30

console.log("original after pop:", nums);
// [10, 20]
Try It Yourself

How It Works

slice(0, -1) returns everything except the last item without touching the source. pop() removes the last item for real.

🚀 Common Use Cases

  • Stack operations — undo/redo, call stacks, DFS.
  • Trim lists — remove the most recently added item.
  • History navigation — pop previous state after push.
  • Processing queues from the end — when order allows LIFO.
  • Drain arrayswhile (arr.length) arr.pop().
  • Pair with push — dynamic stack without a class.

🧠 How pop() Runs

1

Check length

If zero, return undefined immediately.

Guard
2

Read last index

Capture value at length - 1.

Read
3

Decrease length

Array shrinks by one; last slot removed.

Mutate
4

Return removed value

Caller gets the element that was at the end.

📝 Notes

  • Mutates the array—length decreases by 1.
  • Empty array → returns undefined (no throw).
  • Return value is the removed element—store it if needed.
  • O(1) for end removal; shift() at start is slower on large arrays.
  • For immutable updates, use spread/slice: arr.slice(0, -1).
  • ES3—supported everywhere JavaScript runs.

Browser & Runtime Support

Array.prototype.pop() is one of the oldest array methods, available since JavaScript 1.2 / ES3.

Baseline · ES3

Array.prototype.pop()

Supported in every browser ever shipped with JavaScript arrays, including Internet Explorer 3+, and all Node.js versions.

100% Universal support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Array.pop() Universal

Bottom line: Safe to use in any environment. No polyfill ever needed.

Conclusion

pop() removes the last element, returns it, and shortens the array. Use it for stacks and LIFO logic with push(), or guard with length when empty state matters.

Next, learn push() to add elements to the end of an array.

💡 Best Practices

✅ Do

  • Capture the return value when you need the removed item
  • Check length when empty state is meaningful
  • Pair with push for stack behavior
  • Use slice or spread when immutability is required
  • Prefer pop over splice(-1) for clarity

❌ Don’t

  • Assume pop() throws on empty arrays
  • Use pop when you only need to read the last item
  • Forget mutation affects all references to the array
  • Confuse pop with shift (start vs end)
  • Pop inside tight loops on huge arrays without reason

Key Takeaways

Knowledge Unlocked

Five things to remember about Array.pop()

Remove the last element and get it back in one call.

5
Core concepts
📤 02

Returns

Removed item.

Output
03

Mutates

Length - 1.

Side effect
🗃 04

Stack

LIFO + push.

Pattern
05

Empty

undefined.

Edge case

❓ Frequently Asked Questions

pop() removes the last element from an array, shortens the array by one, and returns that removed element.
Yes. pop() changes the array in place by removing the final element and updating length.
undefined. The array stays empty and length remains 0.
push() adds elements to the end; pop() removes the last element. Together they implement a LIFO stack.
pop() removes from the end (last in, first out). shift() removes from the start (first in, first out).
pop() has been available since JavaScript 1.2 / ES3. It works in every browser including very old environments.
Did you know?

pop() and push() are faster at the end of an array than shift() and unshift() at the start, because removing index 0 forces every other element to move down.

Continue to push()

Learn how to append one or more elements to the end of an array.

push() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful