Array Manipulation
Programming
Data Structures
Coding Tips
JavaScript

Remove last item from array

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Removing the last item from an array sounds trivial, but the best method depends on whether you want to mutate the original array or produce a new one. In JavaScript, that usually means choosing between pop() and a non-mutating approach such as slice(0, -1).

That distinction matters because array mutation affects every part of the program that still holds a reference to the same array. In state-driven UI code, that can be the difference between correct updates and subtle bugs.

Use pop() to Mutate the Array

If you want to remove the last element in place, use pop():

javascript
1const numbers = [1, 2, 3, 4, 5];
2const last = numbers.pop();
3
4console.log(numbers); // [1, 2, 3, 4]
5console.log(last);    // 5

pop() does two things:

  • removes the last element from the array
  • returns the removed element

That makes it a natural fit for stack-like behavior.

Use slice() for an Immutable Result

If you want a new array without changing the original one, use slice:

javascript
1const numbers = [1, 2, 3, 4, 5];
2const withoutLast = numbers.slice(0, -1);
3
4console.log(numbers);     // [1, 2, 3, 4, 5]
5console.log(withoutLast); // [1, 2, 3, 4]

This is often the better choice in React state updates, reducers, and other code that prefers immutable transformations.

Handle Empty Arrays Safely

Both approaches behave differently on an empty array:

javascript
1const values = [];
2
3console.log(values.pop());      // undefined
4console.log(values.slice(0, -1)); // []

Neither one crashes, but they communicate different things. pop() returns undefined because there was no last element to remove. slice() simply returns another empty array.

A Reusable Helper

If you want the choice to be explicit at the call site, wrap the behavior in a helper:

javascript
1function removeLast(arr) {
2  return arr.slice(0, -1);
3}
4
5console.log(removeLast(["a", "b", "c"])); // ["a", "b"]

For mutable behavior:

javascript
1function popLast(arr) {
2  if (arr.length === 0) return undefined;
3  return arr.pop();
4}

This makes the mutation policy obvious instead of relying on readers to infer intent from inline code.

Arrays in Other Languages Behave Differently

The title of the problem is generic, but not every language treats arrays the same way. For example:

  • JavaScript arrays are dynamic and support pop()
  • Python lists also support pop()
  • Java arrays are fixed-size, so you create a new shorter array instead
  • C# arrays are fixed-size, while List<T> supports removal

So the "right" answer depends heavily on the language. In JavaScript, pop() and slice() are the usual tools because the array type already supports both mutable and non-mutating patterns naturally.

Common Pitfalls

The biggest mistake is mutating an array with pop() when other code expects the original array to stay unchanged. This is especially common in UI state code and reducer-style logic.

Another common issue is using slice() and assuming it also returns the removed element. It does not. It only returns a new shortened array.

Developers also sometimes forget to handle the empty-array case when the removed value matters. pop() returns undefined, so downstream code should account for that.

Finally, do not generalize one language's solution too broadly. A dynamic JavaScript array and a fixed Java array are not the same problem.

Summary

  • Use pop() when you want to remove the last array item in place.
  • Use slice(0, -1) when you want a new array and need to preserve the original.
  • 'pop() returns the removed item, while slice() returns a new array.'
  • Empty arrays need deliberate handling when the removed value matters.
  • Choose mutation or immutability based on the surrounding design, not just on the shortest syntax.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.