Remove last or last N elements from an Array in JavaScript

avatar
Borislav Hadzhiev

Last updated: Dec 21, 2022
6 min

banner

# Table of Contents

  1. Remove the Last Element from an Array
  2. Remove the Last Element from an Array using Array.slice()
  3. Remove the last N Elements from an Array using Array.slice()
  4. Remove the Last Element from an Array using Array.splice()
  5. Remove the last N Elements from an Array using Array.splice()
  6. Remove the last N Elements from an Array using while loop

# Remove the Last Element from an Array

Use the Array.pop() method to remove the last element from an array, e.g. arr.pop().

The Array.pop() method removes the last element from the array and returns it. The method mutates the original array, changing its length.

index.js
const arr = ['a', 'b', 'c', 'd']; const removeLast = arr.pop(); console.log(removeLast); // ๐Ÿ‘‰๏ธ d console.log(arr); // ๐Ÿ‘‰๏ธ ['a','b','c']

remove last element from an array

We used the Array.pop() method to remove the last element from an array.

If the Array.pop() method is called on an empty array, it doesn't throw an error. Instead, it returns undefined.

index.js
const arr = []; const removeLast = arr.pop(); console.log(removeLast); // ๐Ÿ‘‰๏ธ undefined console.log(arr); // ๐Ÿ‘‰๏ธ []

Changing the contents of the array can be difficult to reason about, especially when you have to mutate the same array multiple times.

Most experienced developers avoid mutating arrays and objects, instead, they create a copy of the array containing only the elements that are needed.

# Remove the Last Element from an Array using Array.slice()

Alternatively, you can use the Array.slice method with a start index of 0 and a stop index of -1.

The Array.slice() method will return a copy of the original array excluding the last element.

index.js
const arr = ['a', 'b', 'c', 'd']; const withoutLast = arr.slice(0, -1); console.log(withoutLast); // ๐Ÿ‘‰๏ธ ['a','b','c'] console.log(arr); // ๐Ÿ‘‰๏ธ ['a', 'b', 'c', 'd']

remove last element from an array using slice

The Array.slice() method doesn't change the original array. Instead, the method returns a new array.

The Array.slice method returns a copy of a portion of an array.

The method takes the following 2 arguments:

NameDescription
start indexThe index of the first element to include in the returned array
end indexThe index of the first element to exclude from the returned array

The Array.slice() method can be passed negative indexes to count backward.

Passing an end index parameter of -1 and arr.length - 1 is the same. We instruct the slice method to go up to, but not including the last element of the array.
index.js
const arr = ['a', 'b', 'c', 'd']; const withoutLast = arr.slice(0, arr.length - 1); console.log(withoutLast); // ๐Ÿ‘‰๏ธ ['a','b','c'] console.log(arr); // ๐Ÿ‘‰๏ธ ['a', 'b', 'c', 'd']

The benefit of this approach is that the contents of the original array remain unchanged.

You can also use the arr.slice() method to remove the last N elements from an array

# Remove the last N Elements from an Array using Array.slice()

To remove the last N elements from an array, call the Array.slice() method with a start index of 0 and an end index of -N.

For example, arr.slice(0, -2) returns a new array that doesn't contain the last 2 elements of the original array.

index.js
const arr = ['a', 'b', 'c', 'd']; // โœ… Remove the last 2 elements from an array const removeLast2 = arr.slice(0, -2); console.log(removeLast2); // ๐Ÿ‘‰๏ธ [ 'a', 'b' ]

remove last n elements from array using slice

A negative end index of -2 means extract up to, but not including the last 2 elements of the array.

Passing an end index of -2 and array.length - 2 is the same.

We instruct the slice method to copy up to, but not including the last 2 elements of the array.

index.js
// โœ… Remove the last 2 elements from an array const arr = ['a', 'b', 'c', 'd']; const removeLast2 = arr.slice(0, -2); console.log(removeLast2); // ๐Ÿ‘‰๏ธ [ 'a', 'b' ] // ----------------------------------------------- // โœ… Same as above (removes last 2 elements from the array) const removeLast2_ = arr.slice(0, arr.length - 2); console.log(removeLast2_); // ๐Ÿ‘‰๏ธ [ 'a', 'b' ]

You can use the same approach to remove the last 3 elements from an array. You just have to specify the end index of -3.

index.js
const arr = ['a', 'b', 'c', 'd']; // โœ… Remove the last 3 elements from an array const withoutLast3 = arr.slice(0, -3); console.log(withoutLast3); // ๐Ÿ‘‰๏ธ [ 'a', 'b' ]

The Array.slice() method doesn't mutate the original array, it returns a new array.

If you try to exclude more elements than are contained in the array, an empty array is returned.

index.js
const arr = ['a', 'b', 'c', 'd']; const removeLast100 = arr.slice(0, -100); console.log(removeLast100); // ๐Ÿ‘‰๏ธ []

We tried to exclude the last 100 elements of an array that only contains 4 elements, so the slice() method returned an empty array.

If you have to do this often, define a reusable function.

index.js
function removeLastN(arr, n) { return arr.slice(0, -n); } const arr = ['a', 'b', 'c', 'd']; console.log(removeLastN(arr, 2)); // ๐Ÿ‘‰๏ธ [ 'a', 'b' ] console.log(removeLastN(arr, 3)); // ๐Ÿ‘‰๏ธ [ 'a' ]

The removeLastN function takes an array and n as parameters and returns a new array that doesn't contain the last N elements of the original array.

If you want to mutate the original array, use the Array.splice() method instead.

# Remove the Last Element from an Array using Array.splice()

Alternatively, you can use the Array.splice() method.

index.js
const arr = ['a', 'b', 'c', 'd']; const last = arr.splice(-1); console.log(last); // ๐Ÿ‘‰๏ธ [ 'd' ] console.log(arr); // ๐Ÿ‘‰๏ธ [ 'a', 'b', 'c' ]

remove last element from array using array splice

The Array.splice method changes the contents of an array by removing or replacing existing elements or adding new elements to the array.

The method takes the following arguments:

NameDescription
startA zero-based index at which to start changing the array
deleteCountThe number of elements to be deleted from the array
item1,..., itemNOne or more values to add to the array from the start index onwards

We only passed a start value of -1 to the Array.splice() method.

A negative start index can be used to count backward.

We start at the second to last element and remove the last element.

The splice() method returns an array containing the removed element.

index.js
const arr = ['a', 'b', 'c', 'd']; const last = arr.splice(-1); console.log(last); // ๐Ÿ‘‰๏ธ [ 'd' ] console.log(arr); // ๐Ÿ‘‰๏ธ [ 'a', 'b', 'c' ]

The method mutates the original array in place, just like the Array.pop() method.

If you need to preserve the original array, use the slice() method instead.

index.js
const arr = ['a', 'b', 'c', 'd']; const withoutLast = arr.slice(0, -1); console.log(withoutLast); // ๐Ÿ‘‰๏ธ ['a','b','c'] console.log(arr); // ๐Ÿ‘‰๏ธ ['a', 'b', 'c', 'd']

Which approach you pick is a matter of personal preference.

The Array.pop() method is the most performant way to remove the last element from an array.

If you don't want to mutate the original array, use the Array.slice() method instead.

# Remove the last N Elements from an Array using Array.splice()

You can also use the Array.splice() method to remove the last N elements from an array.

index.js
const arr = ['a', 'b', 'c', 'd', 'e']; // โœ… Remove the last 2 elements from the array const removedElements = arr.splice(-2); console.log(removedElements); // ๐Ÿ‘‰๏ธ [ 'd', 'e' ] console.log(arr); // ๐Ÿ‘‰๏ธ [ 'a', 'b', 'c' ]

remove last n elements from an array using array splice

The only argument we passed to the splice() method is the start index - the index at which to start removing items.

A negative start index counts back from the end of the array.

For example, passing an index of -2 to the splice() method means "remove the last 2 elements from the array".

index.js
const arr = ['a', 'b', 'c', 'd', 'e']; const removedElements = arr.splice(-2); console.log(removedElements); // [ 'd', 'e' ] console.log(arr); // ๐Ÿ‘‰๏ธ [ 'a', 'b', 'c' ]

Similarly, a start index of -3 can be used to remove the last 3 elements from the array.

index.js
const arr = ['a', 'b', 'c', 'd', 'e']; // โœ… Remove the last 3 elements from the array const removedElements = arr.splice(-3); console.log(removedElements); // [ 'c', 'd', 'e' ] console.log(arr); // ๐Ÿ‘‰๏ธ [ 'a', 'b' ]

If you try to remove more elements than are contained in the array, all elements get removed.

index.js
const arr = ['a', 'b', 'c', 'd', 'e']; const removedElements = arr.splice(-100); console.log(removedElements); // ๐Ÿ‘‰๏ธ [ 'a', 'b', 'c', 'd', 'e' ] console.log(arr); // ๐Ÿ‘‰๏ธ []

We tried to remove the last 100 elements from an array that only contains 5 elements, so the splice() method removed all elements from the array.

Alternatively, you can use a while loop.

# Remove the last N Elements from an Array using while loop

This is a three-step process:

  1. Use a while loop to iterate for as long as N is greater than 0.
  2. On each iteration, decrement N by 1.
  3. Use the Array.pop() method to remove the last element from the array.
index.js
const arr = ['a', 'b', 'c', 'd']; // โœ… remove the last 2 elements from an array let n = 2; while (n > 0) { n -= 1; arr.pop(); } console.log(arr); // ๐Ÿ‘‰๏ธ [ 'a', 'b' ]

We initialized the n variable using the let keyword to be able to reassign it later on.

The while loop iterates for as long as n is greater than 0.

On each iteration, we decrement n by 1 and use the Array.pop() method to remove the last element from the array.

index.js
const arr = ['a', 'b', 'c', 'd']; const removed = arr.pop(); console.log(removed); // ๐Ÿ‘‰๏ธ "d" console.log(arr); // ๐Ÿ‘‰๏ธ [ 'a', 'b', 'c' ]

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

Once the n variable gets set to 0, the condition in the while loop is no longer met.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev