Categories
JavaScript Answers

How to get first N number of elements from an array with JavaScript?

Spread the love

To get first N number of elements from an array with JavaScript, we use the slice method.

For instance, we write

const slicedArray = array.slice(0, n);

to call array.slice with 0 and n to return the first n items in the array array in a new array.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.