Skip to main content

How to Remove the Last Element from an Array in JavaScript

Removing the last element from an array is a common operation, especially when treating an array as a stack or simply manipulating lists of data. JavaScript provides several ways to do this, but it's essential to understand the difference between methods that mutate (modify) the original array and those that create a new array, leaving the original intact.

This guide will demonstrate the best modern methods for removing the last element and the last N elements from an array, with a clear focus on the recommended non-mutating (immutable) approaches.

The Core Task: Mutating vs. Non-Mutating

Before choosing a method, you must decide if you want to change the original array or create a new one.

  • Non-mutating (Immutable): Creates a new array with the desired changes. The original array is left untouched. This is the recommended best practice in modern JavaScript as it leads to more predictable and less error-prone code.
  • Mutating: Modifies the original array in place. This is more memory-efficient as it doesn't create a new array, but it can cause unintended side effects if other parts of your code rely on the original array.

Removing the Last Element

The Array.prototype.slice() method is the perfect tool for creating a new array without the last element. It can take a start and an end index, and using a negative index for the end is a clean way to exclude elements from the end.

Problem: you have an array and want a new array that contains all elements except the last one.

// Problem: Get a new array containing just ['a', 'b'].
const letters = ['a', 'b', 'c'];

Solution:

const letters = ['a', 'b', 'c'];

// slice(0, -1) creates a new array from index 0 up to (but not including) the last element.
const newLetters = letters.slice(0, -1);

console.log(newLetters); // Output: ['a', 'b']
console.log(letters); // Output: ['a', 'b', 'c'] (The original is unchanged)

The Mutating Method: pop()

The Array.prototype.pop() method removes the last element from an array and returns that removed element. This method modifies the original array.

Solution:

const letters = ['a', 'b', 'c'];

const lastElement = letters.pop();

console.log(lastElement); // Output: 'c'
console.log(letters); // Output: ['a', 'b'] (The original is now changed)
note

pop() is useful when you are treating an array like a stack and need to both get and remove the last item in one step.

Removing the Last N Elements

The same principles apply when you need to remove multiple elements from the end of an array.

You can use slice() to remove any number of elements from the end of an array by simply adjusting the negative end index.

Solution:

const numbers = [10, 20, 30, 40, 50];

// Remove the last 3 elements by ending the slice before the last 3 elements.
const newNumbers = numbers.slice(0, -3);

console.log(newNumbers); // Output: [10, 20]
console.log(numbers); // Output: [10, 20, 30, 40, 50] (Unchanged)

The Mutating Method: splice()

The Array.prototype.splice() method can remove elements from any position in an array, modifying it in place. Using a negative start index allows you to count from the end of the array.

array.splice(negativeStartIndex)

Solution:

const numbers = [10, 20, 30, 40, 50];

// A start index of -3 means "start at the 3rd element from the end".
// Omitting the second argument means "delete everything to the end".
const removedElements = numbers.splice(-3);

console.log(removedElements); // Output: [30, 40, 50]
console.log(numbers); // Output: [10, 20] (The original is now changed)

Conclusion

Removing elements from the end of an array is a simple task with clear, modern solutions that let you choose between mutating and non-mutating approaches.

  • To get a new array without the last element(s), the slice() method is the recommended best practice. It is non-mutating and aligns with modern functional programming patterns.
    • array.slice(0, -1) for the last element.
    • array.slice(0, -N) for the last N elements.
  • To modify the original array in place, use pop() to remove the last element or splice() to remove the last N elements. Use these methods with caution, as they can cause side effects.