Create array from 0 to N

Let’s go straight to the point. Sometimes you need an Array instead of a for loop. Here are several ways to do it.

Let’s assume we have already defined our N, as, say, const n = 10; Now here are all our options.

Using spread operator and Array.keys


Using the Array.from static method

Array.from({ length: n }, (_, i) => i)

Using Array.from with

Array.from(Array(n)).map((e, i) => i)

Using Array with .fill and .map

Array(n).fill().map((e, i) => i);

Using underscore library


Did you know that I made an in-browser image converter, where you can convert your images without uploading them anywhere? Try it out and let me know what you think. It's free.

Leave a Comment

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