TL;DR; 3 Different ways to create an array from 1..N in JavaScript

If you are into coding challenges, you will find this very useful. Here are 3 snippets that give you the same results.

Array.from({ length: N }, (_, i) => i + 1); // => [1, 2, 3, 4, .., N]
[...Array(N).keys()].map((i) => i + 1); // => [1, 2, 3, 4, .., N]
[...Array(N).keys()]; // => [0, 1, 2, 3, 4, .., N]
Array.from(Array(N), (_, i) => i + 1); // => [1, 2, 3, 4, .., N]
Array.from(Array(N).keys()); // => [0, 1, 2, 3, 4, .., N]

Credit: https://stackoverflow.com/a/33352604

Greetings! 🌟 Berkcan here, your friendly neighborhood Full Stack Developer. I have a passion for discovering and tinkering with bleeding-edge tech.


Learn three different ways to create an array from 1..N in JavaScript.