How to remove the last item from an array in JavaScript

Removing the last element from JavaScript arrays is essential when building stack-like data structures, managing dynamic content, or implementing undo functionality in user interfaces. With over 25 years of experience in software development and as the creator of CoreUI, I’ve implemented this pattern countless times in components like pagination controls, breadcrumb navigation, and interactive forms where the last item needs to be removed. From my extensive expertise, the most efficient and purpose-built solution is using the pop() method, which removes and returns the last element in a single operation. This approach is optimized, intuitive, and designed specifically for this common use case.

Use the pop() method to remove and return the last item from an array.

const fruits = ['apple', 'banana', 'orange']
const lastFruit = fruits.pop()
// lastFruit: 'orange', fruits: ['apple', 'banana']

The pop() method modifies the original array by removing the last element and returns that removed element. In this example, fruits.pop() removes ‘orange’ from the end of the array and returns it, while the array becomes ['apple', 'banana']. The method returns the removed element, which you can capture in a variable if needed, or simply call fruits.pop() to remove without storing the value. If the array is empty, pop() returns undefined.

Best Practice Note:

This is the same approach we use in CoreUI components to manage navigation history and dynamic content stacks. The pop() method is highly optimized for removing from the end, making it the preferred choice over alternatives like slice(0, -1) when you need to mutate the original array.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
How to Open All Links in New Tab Using JavaScript
How to Open All Links in New Tab Using JavaScript

What is Double Question Mark in JavaScript?
What is Double Question Mark in JavaScript?

How to force a React component to re-render
How to force a React component to re-render

How to concatenate a strings in JavaScript?
How to concatenate a strings in JavaScript?

How to convert a string to boolean in JavaScript
How to convert a string to boolean in JavaScript

What Does javascript:void(0) Mean?
What Does javascript:void(0) Mean?