Last updated: Mar 1, 2024
Reading timeยท3 min

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.
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);

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.
-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.
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.
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.
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.
If you need to get the last N elements, excluding the first, use the slice()
method with Math.max().
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' ]

The Math.max() method returns the larger of the given numbers.
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.
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.
# ๐๏ธ 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.
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' ]

The takeRight() method takes an array and n as parameters and returns the
last N elements of the array.
You can learn more about the related topics by checking out the following tutorials: