JavaScript array pop: remove last element, return value, empty array, slice

Tech reviewed: Deepak Prasad
JavaScript array pop: remove last element, return value, empty array, slice

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.

javascript
let songs = [
  "attention",
  "blinding lights",
  "tell everybody",
  "last last",
  "unstoppable",
];

let removedSong = songs.pop();

console.log(songs, removedSong);
Output

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.

javascript
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);
Output

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.

javascript
const empty = [];
console.log(empty.pop(), empty.length, empty);
Output

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

javascript
const nonArrayObj = {
  length: 4,
  unrelated: "foo",
  3: "three",
};

console.log(Array.prototype.pop.call(nonArrayObj));
console.log(nonArrayObj);
Output

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:

javascript
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);
Output

You should see 3 lines: last 3, then rest [ 1, 2 ], then imm [ 1, 2, 3 ].

With ES2023 toSpliced (non-mutating splice-style):

javascript
const t = [1, 2, 3];
console.log(t.toSpliced(t.length - 1, 1));
console.log(t);
Output

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 mutates length; on an empty array it returns undefined.
  • Loops can call pop repeatedly; pass a count so the loop matches how many elements you remove.
  • Array.prototype.pop.call works on array-like objects with a numeric length; never use pop on strings.
  • For immutable “drop last,” use slice(0, -1) or toSpliced(length - 1, 1); use shift to remove the first element.

References

MDN pages for Array, pop, related mutators, and non-mutating alternatives.


Frequently Asked Questions

1. 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.

2. 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.

3. pop in javascript on an empty array?

pop returns undefined and does not throw. length stays 0.

4. 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.

5. 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).

6. 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.
Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs user-friendly interfaces to enhance digital …

  • JavaScript
  • Web Design