Get last X items from array

Published on August 29th, 2019

javascript icon

Here is yet another one of those JavaScript ‘one line utility’ articles.

Let’s say you array of elements similar to this one, and you want to get last 3 elements from it.

const array = ['first', 'second', 'third', 'fourth', 'fifth']

You’ll have to use JavaScript .splice array method.

const last3 = array.splice(-3, 3) // ['third', 'fourth', 'fifth']

And that’s it! So easy!