Skip to main content

How to Get the Last N Elements of an Array in JavaScript

A common requirement in data manipulation is to get a "slice" from the end of an array. You might need to get the last 5 transactions, the 10 most recent log entries, or simply a subset of an array starting from the end. The most direct and readable way to do this in JavaScript is with the Array.prototype.slice() method.

This guide will teach you how to use the slice() method with negative indexes to get the last N elements of an array. You will also learn the best way to get just the single last element.

The slice() method is the idiomatic and performant way to get a portion of an array. It returns a new array and does not modify the original. Its key feature for this task is its ability to accept negative indexes.

The Logic When you provide a negative index to slice(), it counts from the end of the array. slice(-N) will start the slice N elements from the end and continue to the end of the array.

For example, we have an array and we want to create a new array containing only its last three elements.

// Problem: How to get ['c', 'd', 'e'] from this array?
const letters = ['a', 'b', 'c', 'd', 'e'];

Solution:

const letters = ['a', 'b', 'c', 'd', 'e'];

// Get the last 3 elements
const lastThree = letters.slice(-3);
console.log(lastThree); // Output: ['c', 'd', 'e']

// Get the last 2 elements
const lastTwo = letters.slice(-2);
console.log(lastTwo); // Output: ['d', 'e']

// The original array remains unchanged
console.log(letters); // Output: ['a', 'b', 'c', 'd', 'e']

Output

['c', 'd', 'e']
['d', 'e']
['a', 'b', 'c', 'd', 'e']
note

This is the recommended best practice for its clarity, conciseness, and performance.

Handling Edge Cases: The slice() method is very safe. If you ask for more elements than the array contains, it will simply return a copy of the entire array without throwing an error.

const shortArray = ['a', 'b'];
const result = shortArray.slice(-5);
console.log(result); // Output: ['a', 'b']

Getting Just the Single Last Element

If you only need the single last element of an array (not an array containing that element), there are more direct methods than slice(-1).

The Solution (Modern): The at() Method

The Array.prototype.at() method was introduced in ES2022 and is the cleanest way to get an element from the end of an array.

const letters = ['a', 'b', 'c', 'd', 'e'];
const lastElement = letters.at(-1);
console.log(lastElement); // Output: "e"

The Solution (Classic)

The traditional way is to use the length property.

const letters = ['a', 'b', 'c', 'd', 'e'];
const lastElement = letters[letters.length - 1];
console.log(lastElement); // Output: "e"
note

For getting just the last element, .at(-1) is the most readable modern approach.

A Note on an Inefficient Alternative to Avoid

While you could technically use other methods like filter() or reduce() to achieve this, they are considered anti-patterns for this task. The slice() method is highly optimized for this specific operation, whereas a method like filter() would needlessly iterate over the entire array. Always prefer slice() for getting a subset from the start or end of an array.

Conclusion

Getting the last N elements of an array is a simple task with a clear best practice.

  • The array.slice(-N) method is the definitive, recommended solution for creating a new array containing the last N elements. It is concise, readable, performant, and safe.
  • If you only need the single last element, use the modern .at(-1) method for the cleanest code.