EN
JavaScript - get last n elements of array
0
points
In this article, we would like to show you how to get the last n elements in an array in JavaScript.
Practical example
To get the last n elements from the array, we use slice() method.
// ONLINE-RUNNER:browser;
const array = ['a', 'b', 'c', 'd'];
const n = 3;
const result = array.slice(-n);
console.log(result); // [ 'b', 'c', 'd' ]