Get the Last N Elements of an Array in JavaScript

There are many ways to get the last element of an array in JavaScript. In this article, we will discuss two of them using the slice() and splice() methods.

1. Using the slice() Method

The slice() method is used to get a portion of an array.

It takes two arguments: the start index and the end index. The start index is inclusive, and the end index is exclusive.

To get the last N elements of an array using slice() method, pass the -n as a first parameter (start index). 

✅ The slice() method does not modify the original array and returns a shallow copy of the array.

For example, get the last 5 elements of an Array:

JavaScript
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const last5 = arr.slice(-5);
console.log(last5); //👉 [ 6, 7, 8, 9, 10 ]

We passed one parameter(start index) -5 to get the last 5 elements of an array.

When we pass a negative number as a parameter to the slice() method, it indicates an offset from the end of the array. For example, -5 returns the last 5 elements of an array.

Alternatively, you can pass array.length - n to the slice() method to get the same result.

JavaScript
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const last5 = arr.slice(arr.length - 5);
console.log(last5); //👉 [ 6, 7, 8, 9, 10 ]

Both ways tell the slice() method to copy the last five elements of an array and create a new array to store it.

✅ It will not throw an error even if we try to get more elements than the array length. Instead, it returns a new array with all elements.

JavaScript
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const last20 = arr.slice(-20);
console.log(last20); //👉 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

We tried to get the last 20 elements of an array which contains only 10 elements. In such a case, it copies all array elements and returns.


2. Using the splice() method

⚠️ The splice() method removes elements from an array and returns the removed elements as an array.

It takes three arguments: the start index, the number of elements to remove, and (optionally) the elements to add. The start index is inclusive, and the number of elements to remove is the number of elements from the start index to the end of the array.

To get the last N elements of an array using splice() method, pass the -n  or array.length - n as a first parameter (start index). 

JavaScript
//1. Using -n 

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const last5 = arr.splice(-5);
console.log(last5); //👉 [ 6, 7, 8, 9, 10 ]
console.log(arr); //👉 [ 1, 2, 3, 4, 5 ]

//2. Using array.length - 5
 
const arr2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const last5_2 = arr2.splice(arr2.length - 5);
console.log(last5_2); //👉 [ 6, 7, 8, 9, 10 ]
console.log(arr2); //👉 [ 1, 2, 3, 4, 5 ]

⚠️ The splice() method should be used with caution as it has the side effect of modifying the original array.

✅ It will not throw an error even if we try to get more elements than the array length. Instead, it removes all elements and returns them as new array.

JavaScript
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const last20 = arr.splice(-20);
console.log(last20); //👉 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
console.log(arr); //👉 []

We tried to get the last 20 elements of an array that contains only 10 elements. In such a case, it removes all elements of the array and returns them as new array.


Conclusion

There are multiple ways to get the last N elements of an array in JavaScript. In this article, we discussed two methods – the slice() and splice() methods. Both methods are effective in getting the desired results.

However, it is important to note that the slice() method is a static method, while the splice() method is a mutable method. This means that the slice() method returns a new array while the splice() method modifies the existing array.


Learn More:

Unlock your programming potential with Stack Thrive! Discover step-by-step tutorials, insightful tips, and expert advice that will sharpen your coding skills and broaden your knowledge.

Leave a Comment

Facebook Twitter WhatsApp