Categories
JavaScript Answers

How to create an array containing 1…N with JavaScript?

Spread the love

To create an array containing 1…N with JavaScript, we use the array keys method.

For instance, we write

const a = Array.from(Array(10).keys());

to call Array(10).keys() to get an array of indexes of the array with length 10.

Then we use Array.from to create a copy of the array and return it.

By John Au-Yeung

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

Leave a Reply

Your email address will not be published.