Get the last N number of elements from an array in JavaScript

Today we’ll show you how to get the last N number of elements from an array in JavaScript. There are multiple ways to get last N number of items from an array using JavaScript.

In the previous article, we explained to you how to get the first N number of elements from an array. You may check the few more articles related to the Array.

Ways to get the last N number of elements from an array

  1. slice() method
  2. splice() method

1. slice() method

In this method, we will use the slice() method to get the last 5 items from an array. It is a very simple and most preferable method to get the last 5 elements from the list.

Example 1: Get the last 5 elements

Example 2: Get the last 2 elements

2. splice() method

Using the splice() method, we can also get the last N number of elements from the list. But it will update the original array by removing the last N number of elements from the list.

Example 1: Get the last 5 elements and update original list

Example 2: Get the last 2 elements and update original list

That’s it for today.
Thank you for reading. Happy Coding..!! 🙂

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *