Get last X elements

Last update in 8/2019

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!