EN
JavaScript - get last n elements from array excluding first element
0
points
In this article, we would like to show you how to get the last n elements from the array excluding the first element in JavaScript.
Practical example
To get the last n elements from the array excluding the first element, we use slice() method with the second argument set to 1.
The Math.max() method is used to get all elements if the is less than the number of elements that we want to get.array.length
// ONLINE-RUNNER:browser;
const array = ['a', 'b', 'c', 'd', 'e'];
const n = 3;
const result = array.slice(Math.max(array.length - n, 1));
console.log(result); // [ 'c', 'd', 'e' ]