Remove The Last Two Elements From An Array In JavaScript

To remove the last two elements from an array in JavaScript, you can use any one of the following methods –

  1. Use the Array.splice() method
  2. Use the Array.slice() method
  3. Use the pop() method twice

Let’s take a look at each of these methods in detail.

remove the last two elements from an array in JavaScript

1) Array.splice() To Remove The Last Two Elements From An Array In JavaScript

The Array.splice() method is used to remove elements from an array and can also be used to add new elements at the same time.

This method takes three arguments – the index at which to start changing the array (with the deleteCount taken into account), the number of elements to delete, and the elements to add (optional) – in that order.

Therefore, to remove the last two elements from an array, we can pass the index as the second last index of the array, and the deleteCount as 2.

Given below is the code to remove last two elements from an array using Array.splice() method –

var arr = ["JavaScript", "is", "awesome"];

arr.splice(-2, 2);

console.log(arr); // Output: ['JavaScript']

In the above method, we have passed the index as -2 and deleteCount as 2.

The negative value for the index means that it starts counting from the end of the array. Therefore, since we want to remove the last two elements, we have passed -2 (second last index) as the starting point for splice().

A deleteCount of 2 means that it will delete two elements starting from the given index. So the above method removes two elements from the array starting from the second last index.

-2 is same as arr.length-2, so you can actually use that as well.

2) Array.slice() To Remove Last Two Elements From An Array In JavaScript

The Array.slice() method is used to create a shallow copy of a part of an array.

This method takes two arguments – the start index (required), and the end index (optional). It returns a new array that contains the elements from the original array starting from the given start index up to, but not including, the given end index.

Therefore, to remove the last two elements from an array using this method, we can pass 0 as the starting index, and -2 as the end index.

Given below is the code to remove last two elements from an array using Array.slice() method –

var arr = ["JavaScript", "is", "awesome"];

arr = arr.slice(0, -2);

console.log(arr); // Output: ['JavaScript']

In the above method, we have passed 0 as the start index and -2 as the end index.

The negative value for the end index means that it starts counting from the end of the array. Therefore, since we want to remove the last two elements, we have passed -2 (second last index) as the ending point for slice().

A start index of 0 means that it will start copying the elements from the beginning of the array. So the above method copies all elements except for the last two.

Note that the slice() method doesn’t change the original array.

3) Using pop() Method Twice To Remove Last Two Elements From An Array

Another way to remove the last two elements from an array in JavaScript is by using the pop() method twice.

The pop() method removes the last element from an array and returns that element. Therefore, if we use this method twice, it will remove the last two elements from an array.

Given below is the code to remove last two elements from an array using pop() method –

var arr = ["JavaScript", "is", "awesome"];

arr.pop();

arr.pop();

console.log(arr); // Output: ['JavaScript']

In the above method, we have used the pop() method twice to remove two elements from the array – first to remove “awesome” and then to remove “is”.

This method works because when we call the pop() method on an array, it removes the last element of that array and returns it. Since we don’t need that element, we are not storing the return value in a variable.

Therefore, calling the pop() method twice will remove the last two elements from an array.

Conclusion

In this article, we saw three ways to remove last two elements from an array in JavaScript.

While all three methods are quite similar, the method you choose to use will depend on your specific needs.

If you do not wish to modify the original array, then you can use the slice() method.

If you are okay with modifying the original array, then you can use either the splice() or pop() method.

Do let us know your thoughts in the comments below. And don’t forget to share this article with others who might find it helpful.

Thanks for reading! 🙂

Leave a Reply