Last updated: Mar 1, 2024
Reading timeยท6 min
while
loopUse 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.
const arr = ['a', 'b', 'c', 'd']; const removeLast = arr.pop(); console.log(removeLast); // ๐๏ธ d console.log(arr); // ๐๏ธ ['a','b','c']
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
.
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.
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.
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']
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:
Name | Description |
---|---|
start index | The index of the first element to include in the returned array |
end index | The index of the first element to exclude from the returned array |
The Array.slice()
method can be passed negative indexes to count backward.
-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.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
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.
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' ]
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.
// โ 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
.
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.
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.
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.
Alternatively, you can use the Array.splice()
method.
const arr = ['a', 'b', 'c', 'd']; const last = arr.splice(-1); console.log(last); // ๐๏ธ [ 'd' ] console.log(arr); // ๐๏ธ [ 'a', 'b', 'c' ]
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:
Name | Description |
---|---|
start | A zero-based index at which to start changing the array |
deleteCount | The number of elements to be deleted from the array |
item1,..., itemN | One 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.
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.
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.
You can also use the Array.splice()
method to remove the last N elements from
an array.
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' ]
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".
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.
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.
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 contained 5
elements, so the splice()
method removed all elements from the array.
Alternatively, you can use a while
loop.
while
loopThis is a three-step process:
while
loop to iterate for as long as N is greater than 0.1
.Array.pop()
method to remove the last element from the array.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.
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.