Array.prototype.pop() removes the last element from an array, shortens length by one, and returns that element—or undefined when the array was already empty. Because it mutates the array in place, every reference to the same array sees the change. To remove the first element instead, use shift; to return a shallow copy without the last element, combine slice with patterns from JavaScript array slicing.
Tested on: Node.js v20.18.2. A short note after each snippet describes what you should see in the console.
Quick reference
pop is the mutating “drop last” primitive; when you must preserve the original reference for other readers, use slice(0, -1) or toSpliced.
| Goal | API |
|---|---|
| Remove last, mutate | arr.pop() |
| Remove last, new array | arr.slice(0, -1) or arr.toSpliced(arr.length - 1, 1) |
| Remove first, mutate | arr.shift() |
shift() drops the first element from the front; the
remove element from array in JavaScript
guide compares pop, shift, splice, and filter when the removal
index changes.
Basic array pop: return value and mutated array
pop returns the removed element and updates the array in place.
let songs = [
"attention",
"blinding lights",
"tell everybody",
"last last",
"unstoppable",
];
let removedSong = songs.pop();
console.log(songs, removedSong);You should see one line like: [ 'attention', 'blinding lights', 'tell everybody', 'last last' ] unstoppable.
Pop multiple times (pop javascript loop)
The loop uses count so removeSongs(3) removes three items from the end in order.
let songs = [
"attention",
"blinding lights",
"tell everybody",
"last last",
"unstoppable",
];
function removeSongs(count = 1) {
for (let index = 0; index < count; index++) {
let removedSong = songs.pop();
console.log(
`This song - ${removedSong} - has been removed from your library.`
);
}
}
removeSongs(3);
console.log(songs);You should see 4 lines: This song - unstoppable - has been removed from your library., then This song - last last - has been removed from your library., then This song - tell everybody - has been removed from your library., then [ 'attention', 'blinding lights' ].
Empty array (js pop)
On an empty array, pop returns undefined and leaves length at 0.
Before a loop keeps calling pop, use
empty-array checks in JavaScript so
you stop once length reaches zero.
const empty = [];
console.log(empty.pop(), empty.length, empty);You should see one line like: undefined 0 [].
Generic array pop js on array-like objects
pop is generic: it reads this.length, returns and deletes the property at length - 1, then decrements length. Do not use pop on strings—they are immutable (MDN).
const nonArrayObj = {
length: 4,
unrelated: "foo",
3: "three",
};
console.log(Array.prototype.pop.call(nonArrayObj));
console.log(nonArrayObj);You should see 2 lines: three, then { length: 3, unrelated: 'foo' }.
Immutable last-element patterns (array pop javascript without mutation)
Read the last value and build a new array without changing the original:
const imm = [1, 2, 3];
const last = imm.at(-1);
const rest = imm.slice(0, -1);
console.log("last", last);
console.log("rest", rest);
console.log("imm", imm);You should see 3 lines: last 3, then rest [ 1, 2 ], then imm [ 1, 2, 3 ].
With ES2023 toSpliced (non-mutating splice-style), the
JavaScript splice() guide covers the mutating
version that toSpliced mirrors:
const t = [1, 2, 3];
console.log(t.toSpliced(t.length - 1, 1));
console.log(t);You should see 2 lines: [ 1, 2 ], then [ 1, 2, 3 ].
Summary
pop shortens length in place and returns the removed value (or undefined on an empty array); pair that mental model with slice / toSpliced when you need non-destructive “drop last.”
pop()removes the last element, returns it, and mutateslength; on an empty array it returnsundefined.- Loops can call
poprepeatedly; pass a count so the loop matches how many elements you remove. Array.prototype.pop.callworks on array-like objects with a numericlength; never usepopon strings.- For immutable “drop last,” use
slice(0, -1)ortoSpliced(length - 1, 1); useshiftto remove the first element.
References
MDN pages for Array, pop, related mutators, and non-mutating alternatives.
- MDN:
Array - MDN:
Array.prototype.pop() - MDN:
Array.prototype.shift() - MDN:
Array.prototype.slice() - MDN:
Array.prototype.toSpliced()
Frequently Asked Questions
What does javascript pop return?
It returns the element that was removed from the end. If the array is empty, it returns undefined and leaves length at 0.
Does array pop javascript code mutate the array?
Yes. pop changes the array in place and shortens length. For a new array without mutating the original, use slice(0, -1) or toSpliced(length-1, 1) instead.
pop in javascript on an empty array?
pop returns undefined and does not throw. length stays 0.
What is the difference between pop javascript and shift?
pop removes the last element; shift removes the first. Both mutate and return the removed value, or undefined when there is nothing to remove.
Can I use pop js style on non-array objects?
Yes. pop is generic: it uses this.length and deletes the property at length-1 after decrementing length. The object must look like an array (integer keys and length).
How do I remove the last n elements with js pop in a loop?
Call pop repeatedly, or use a for loop from 0 to n-1. Each call removes one element from the end.
