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.
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():
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:
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:
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:
For mutable behavior:
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, whileslice()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
- Remove multiple keys from Map in efficient way?
- Remove nodes from graph or reset entire default graph
- remove None value from a list without removing the 0 value
- Removing an activity from the history stack
- Removing input background colour for Chrome autocomplete?
- Rendering Plaintext as HTML maintaining whitespace – without pre
- Removing an element from an Array Java
- Removing duplicate elements from an array in Swift

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