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"]
const removedElement = array.pop();
pop() takes no arguments. Passing arguments does not choose which item to remove; it always targets the last array element.
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.
const values = [];
const removed = values.pop();
console.log(removed); // undefined
console.log(values.length); // 0
The array remains empty and pop() returns undefined.
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.
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
const tasks = ["A", "B", "C"];
while (tasks.length > 0) {
console.log(tasks.pop());
}
C
B
A
This intentionally empties the array from the end.
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
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]
pop() to return the new array; it returns the removed element.pop(index) to remove a chosen index. Use splice() for position-based removal.pop() just to inspect the last value and accidentally mutating the array.Open pop() Demo Array length Remove the First Element with shift() Remove All Elements Display Array Elements
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.