Last n elements

Angelos Chalaris · JavaScript, Array · Jul 23, 2022

Gets the last n elements of an array.

const lastN = (arr, n) => arr.slice(-n);
lastN(['a', 'b', 'c', 'd'], 2); // ['c', 'd']

Recommended snippets