JavaScript Array pop() Method

pop() removes the last element from a JavaScript array and returns that removed value. It changes the original array and reduces its length by one.

const scripts = ["PHP", "ASP", "JavaScript", "HTML"];
const removed = scripts.pop();

console.log(removed); // HTML
console.log(scripts); // ["PHP", "ASP", "JavaScript"]

pop() Syntax and Return Value Top ↑

const removedElement = array.pop();

pop() takes no arguments. Passing arguments does not choose which item to remove; it always targets the last array element.

How pop() Changes Array length Top ↑

const values = [10, 20, 30];
console.log(values.length); // 3
values.pop();
console.log(values.length); // 2

The remaining indexes stay in the same order because only the end of the array is removed.

What pop() Returns for an Empty Array Top ↑

const values = [];
const removed = values.pop();
console.log(removed);      // undefined
console.log(values.length); // 0

The array remains empty and pop() returns undefined.

Use push() and pop() for LIFO Stack Behavior Top ↑

const stack = [];
stack.push("first");
stack.push("second");
stack.push("third");

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

This last-in, first-out pattern is useful for undo histories, traversal stacks, and other stack-like tasks.

Array Indexes After pop() Top ↑

The original page demonstrated numeric indexes after removal. Existing earlier indexes do not shift when the last item is removed.

const scripts = ["PHP", "ASP", "JavaScript", "HTML"];
scripts.pop();

for (const [index, value] of scripts.entries()) {
  console.log(index, value);
}
0 PHP
1 ASP
2 JavaScript

Remove Elements Repeatedly with pop() Top ↑

const tasks = ["A", "B", "C"];

while (tasks.length > 0) {
  console.log(tasks.pop());
}
C
B
A

This intentionally empties the array from the end.

Read the Last Element Without Removing It Top ↑

If you only need to inspect the last value, do not call pop().

const scripts = ["PHP", "JavaScript", "Python"];
console.log(scripts[scripts.length - 1]); // Python
console.log(scripts.at(-1));              // Python
console.log(scripts.length);              // 3

Create a Version Without the Last Element Top ↑

If you want a new array without mutating the original, slice(0, -1) is a simple option.

const original = [1, 2, 3];
const withoutLast = original.slice(0, -1);

console.log(original);    // [1, 2, 3]
console.log(withoutLast); // [1, 2]

Common pop() Mistakes Top ↑

  • Expecting pop() to return the new array; it returns the removed element.
  • Calling pop(index) to remove a chosen index. Use splice() for position-based removal.
  • Using pop() just to inspect the last value and accidentally mutating the array.
  • Forgetting that all references to the same array see the removal.

Array pop() Demo Top ↑

Open pop() Demo Array length Remove the First Element with shift() Remove All Elements Display Array Elements




Subscribe to our YouTube Channel here



plus2net.com










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer