How To Remove The Last Item from An Array In JavaScript – Definitive Guide

JavaScript arrays allow you to store multiple items in a single object.

You can remove the last item from an array in JavaScript using the array.pop() method.

Basic Example

const birdsArray = ["dove", "parrot", "finch"];

const lastElement = birdsArray.pop();

console.log(lastElement);

console.log(birdsArray);

Output

The last element is removed from the original array and displayed first. The other two elements remain the same in the array.

finch

['dove', 'parrot']

This tutorial teaches you the different methods available to remove the last item from an array in JavaScript and when it is appropriate to use each method.

Remove The Last Item from An Array Using Array Pop Method

The pop() method removes the last element from the array and returns it.

  • It mutates the same array object
  • The size of the array is changed

Return Value

Returns the last element from the array after removing it.

When the array is empty, it returns undefined and doesn’t throw any exception.

Code

The following code demonstrates how to use the pop() method to remove the last element from the array.

Also, it shows how the original array object is modified when the pop() method is used.

const birdsArray = ["dove", "parrot", "finch"];

const lastElement = birdsArray.pop();

console.log(lastElement);

console.log(birdsArray);

Output

The last element is removed from the original array and displayed first. The other two elements remain the same in the array.

finch

['dove', 'parrot']

Remove The Last Item from An Array Using Array Slice Method

This section teaches you how to use the slice() method to remove the last element from an array in JavaScript.

The slice() method returns the selected elements from the array from the specified start to the end index as a new array object.

Use the slice() method when you want to select specific items from the array and create a new object instead of mutating the same object.

Code

The following code demonstrates how to use the slice() method to remove the last element from an array.

A new array object is created without the last element of the existing array.

const birdsArray = ["dove", "parrot", "finch"];

const arrayWithoutLastElement = birdsArray.slice(0, -1);

console.log(arrayWithoutLastElement);

console.log("The original array is unchanged as follows:”);

console.log(birdsArray);

Output

A new array is created without the last element, and the existing array remains unchanged.

['dove', 'parrot']

The original array is unchanged as follows:

['dove', 'parrot', 'finch']

Remove The Last Item from An Array Using Splice Method

The splice() method modifies the content of the existing array by removing or replacing the existing elements.

Use the splice() method when you want to remove a range of items from the existing array instead of creating a new array.

Code

The following code demonstrates how to use the splice() method to remove the last element from an array.

The existing array object is modified to remove the last item from it.

const birdsArray = ["dove", "parrot", "finch"];

const arrayWithoutLastElement = birdsArray.splice(birdsArray.length - 1);

console.log(arrayWithoutLastElement);

console.log("The original array is changed as follows");

console.log(birdsArray);

Output

The last element is removed from the existing array itself.

['finch']

The original array is changed as follows

['dove', 'parrot']

Remove Last N Element From An Array in JavaScript

This section teaches you how to remove the last N elements from an array in JavaScript. You can use the slice() method to remove the last N elements from an array.

Use the end index as -2 to remove the last two elements from the array and create a new array with only the remaining elements.

While using this method, the original array remains unchanged.

Code

The following code demonstrates how to remove the last two elements from an array using the slice() method.

const birdsArray = ["dove", "parrot", "finch", "owl"];

const arrayWithoutLast2Elements = birdsArray.slice(-2);

console.log(arrayWithoutLast2Elements);

console.log("The original array is unchanged as follows");

console.log(birdsArray);

Output

['dove', 'parrot']

['dove', 'parrot', 'finch', 'owl']

Additional Resources

Leave a Comment