Last n elements
JavaScript, Array · Jul 23, 2022

Gets the last n
elements of an array.
- Use
Array.prototype.slice()
with a start value of-n
to get the lastn
elements ofarr
.
const lastN = (arr, n) => arr.slice(-n);
lastN(['a', 'b', 'c', 'd'], 2); // ['c', 'd']