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() |
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.
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):
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()
