Get the last N elements of an Array in JavaScript

avatar
Borislav Hadzhiev

Last updated: Dec 18, 2022
3 min

banner

# Get the last N elements of an Array

Use the Array.slice() method to get the last N elements of an array, e.g. const last3 = arr.slice(-3).

The Array.slice() method will return a new array containing the last N elements of the original array.

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

get last n elements of array

The only argument we passed to the Array.slice method is the start index.

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

Passing a negative index indicates an offset from the end of the array. A negative index of -3 means "give me the last 3 elements of the array".

This is the same as passing an argument of array.length - 3 to the slice() method.

index.js
const arr = ['a', 'b', 'c', 'd', 'e']; const last3 = arr.slice(-3); console.log(last3); // ๐Ÿ‘‰๏ธ ['c', 'd', 'e'] const last3Again = arr.slice(arr.length - 3); console.log(last3Again); // ๐Ÿ‘‰๏ธ ['c', 'd', 'e']

Either way, we tell the slice() method to copy the last 3 elements of the array and place them in a new array.

The Array.slice() method doesn't mutate the original array. It returns a new array with the copied elements (a shallow copy of a portion of the original array).

Even if we try to get more elements than the array contains, Array.slice() won't throw an error.

Instead, it returns a new array with all elements of the original array.

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

We tried to get the last 100 elements of an array that only contains 3 elements.

In this case, all elements of the original array get copied to the new array.

# Getting the last N elements, excluding the first

If you need to get the last N elements, excluding the first, use the slice() method with Math.max().

index.js
const arr = ['a', 'b', 'c']; // ๐Ÿ‘‡๏ธ get last 3 elements, excluding the first console.log(arr.slice(Math.max(arr.length - 3, 1))); // [ 'b', 'c' ] // ๐Ÿ‘‡๏ธ get last 2 elements, excluding the first console.log(arr.slice(Math.max(arr.length - 2, 1))); // [ 'b', 'c' ] // ๐Ÿ‘‡๏ธ get last element, excluding the first console.log(arr.slice(Math.max(arr.length - 1, 1))); // [ 'c' ]

getting the last n elements excluding the first

The Math.max() method returns the larger of the given numbers.

index.js
console.log(Math.max(5, 1)); // 5 console.log(Math.max(3, 1)); // 3 console.log(Math.max(0, 1)); // 1 console.log(Math.max(-1, 1)); // 1

We used the method to specify a min index of 1.

This way, the array slice returns the last N elements of the array, excluding the first item.

# Get the last N elements of an array using lodash

You can also use the lodash module to get the last N elements of an array.

First, make sure you have lodash installed by running the following command from your terminal.

shell
# ๐Ÿ‘‡๏ธ initialize package.json if you don't have one npm init -y npm install lodash

Now you can import and use the takeRight method to get the last N elements.

index.js
import _ from 'lodash'; const arr = ['a', 'b', 'c', 'd', 'e']; console.log(_.takeRight(arr, 4)); // [ 'b', 'c', 'd', 'e' ] console.log(_.takeRight(arr, 3)); // [ 'c', 'd', 'e' ] console.log(_.takeRight(arr, 2)); // [ 'd', 'e' ]

get last n elements of array using lodash

The takeRight method takes an array and n as parameters and returns the last N elements of the array.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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 ยฉ 2023 Borislav Hadzhiev