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.
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
xxxxxxxxxx
1
const array = ['a', 'b', 'c', 'd', 'e'];
2
const n = 3;
3
4
const result = array.slice(Math.max(array.length - n, 1));
5
6
console.log(result); // [ 'c', 'd', 'e' ]