Array.prototype.pop()
pop() Returns:
any · Added in vES5 · Updated March 13, 2026 · Array Methods array pop mutating
pop() removes the last element from an array and returns that element. It modifies the array in place, reducing its length by 1. This method is the counterpart to push() and together they implement a stack data structure.
Syntax
arr.pop()
Takes no parameters. Returns the removed element, or undefined if the array is empty.
Examples
Basic usage
const fruits = ["apple", "banana", "cherry"];
const last = fruits.pop();
console.log(last); // "cherry"
console.log(fruits); // ["apple", "banana"]
Working with empty arrays
const empty = [Global_Objects::eval];
const result = empty.pop();
console.log(result); // undefined
console.log(empty); // [Global_Objects::eval]
Processing elements from the end
const items = ["a", "b", "c"];
while (items.length > 0) {
console.log(items.pop());
}
// Output: c, b, a
Clearing an array efficiently
const arr = [1, 2, 3, 4, 5];
while (arr.pop()) {
// removes elements until array is empty
}
console.log(arr); // [Global_Objects::eval]
Stack Implementation
Arrays with pop() naturally form a last-in-first-out (LIFO) stack:
class Stack {
constructor() {
this.items = [Global_Objects::eval];
}
push(item) {
this.items.push(item);
}
pop() {
return this.items.pop();
}
peek() {
return this.items[this.items.length - 1];
}
isEmpty() {
return this.items.length === 0;
}
}
const stack = new Stack();
stack.push(1);
stack.push(2);
stack.push(3);
console.log(stack.pop()); // 3
console.log(stack.pop()); // 2
console.log(stack.isEmpty()); // false
Practical: parsing delimited string
function parseCSV(csv) {
const result = [Global_Objects::eval];
let current = '';
for (const char of csv) {
if (char === ',') {
result.push(current);
current = '';
} else {
current += char;
}
}
// Don't forget the last field
result.push(current);
return result;
}
console.log(parseCSV("a,b,c,d")); // ['a', 'b', 'c', 'd']
Removing falsy values from end
const data = [1, 2, 3, null, undefined, false];
while (data.length > 0 && !data[data.length - 1]) {
data.pop();
}
console.log(data); // [1, 2, 3]
Getting array without last element
const items = [1, 2, 3, 4, 5];
const allButLast = [...items];
allButLast.pop();
console.log(allButLast); // [1, 2, 3, 4]
Behavior Notes
- Returns
undefinedon empty arrays — always check length if unsure - Returns the removed element, not the new array
- This method modifies the original array (mutating method)
- Works on array-like objects with length property
Performance
pop() is O(1) constant time — it simply decrements the array length. Unlike shift() which must move all remaining elements, pop() is very efficient.
See Also
- array::shift — Remove the first element
- array::unshift — Add elements to the start
- array::splice — Add/remove elements at any position