How to Get The Last N Elements Of An Array In JavaScript

Don’t worry if you encounter the requirement to get the last N elements of an array in JavaScript because we will share with you how to do that in this article. Let’s go.

How to get the last N elements of an array in JavaScript

The solution to getting the last N elements of the array we share in this article is to use the slice() method.

The slice() method selects the elements in an array from the specified start position to the end position (excluding the end position). It then returns a new array containing the selected elements and does not change the original array.

Syntax:

array.slice(start, end)

Parameter:

start: a position to start selecting. The default is 0. If the start is negative, the slice() method will count from the end of the array.

end: a position to end select. The default is the length of the array. If the end is negative, the slice() method will count from the end of the array.

The following example demonstrates how the slice() method works in some cases.

Example:

// Create an array
const color = new Array("Yellow", "Red", "Blue", "Black", "Brown");

console.log("Select all elements");
console.log(color.slice());

console.log("\nThe start and end arguments are positive numbers");
console.log(color.slice(1, 4));

console.log("\nThe end argument is a negative number");
console.log(color.slice(2, -1));

Output:

Select all elements
[ 'Yellow', 'Red', 'Blue', 'Black', 'Brown' ]

The start and end arguments are positive numbers
[ 'Red', 'Blue', 'Black' ]

The end argument is a negative number
[ 'Blue', 'Black' ]

We’ll just set the start argument to a negative integer to the slice() method and omit the end argument to get the last N elements of an array.

Example:

// Create an array
const color = new Array("Yellow", "Red", "Blue", "Black", "Brown");

console.log("Get the last 3 elements of the 'color' array");
console.log(color.slice(-3));

console.log("\nGet the last 4 elements of the 'color' array");
console.log(color.slice(-4));

Output:

Get the last 3 elements of the 'color' array
[ 'Blue', 'Black', 'Brown' ]

Get the last 4 elements of the 'color' array
[ 'Red', 'Blue', 'Black', 'Brown' ]

One interesting thing is that if we try to get some element more than the length of the array, the slice() method will not return any error.

Example:

// Create an array
const color = new Array("Yellow", "Red", "Blue", "Black", "Brown");

console.log("Get the last 8 elements of the 'color' array");
console.log(color.slice(-8));

Output:

Get the last 8 elements of the 'color' array
[ 'Yellow', 'Red', 'Blue', 'Black', 'Brown' ]

In this example, the slice() method returned all the array elements instead of returning an error.

Click here to learn more about JavaScript Array methods.

Summary

This article shared how to get the last N elements of an array in JavaScript using the slice() method. You must set the start argument to a negative value and omit the end argument. Thank you for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *