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

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

You may check the few more articles related to the Array.

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

  1. slice() method
  2. filter() method

1. slice() method

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

Example 1: Get the first 5 elements

Example 2: Get the first 2 elements

2. filter() method

Using the filter() method, we can also get the first N number of elements from the list. Here we need to use the index of elements to filter the records.

Example 1: Get the first 5 elements

Example 2: Get the first 2 elements

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 *