DEV Community

Justin
Justin

Posted on • Edited on

JS Challenge: Create an array and fill with serial numbers

The following code creates and fills an array with numbers 0 to 9

[...Array(10).keys()]
Enter fullscreen mode Exit fullscreen mode

Result: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

If you want the numbers from 1 to 10:

[...Array(10).keys()].map(i => i+1)
Enter fullscreen mode Exit fullscreen mode

Result: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Or this should work as well:

Array(10).fill().map((v,i) => i+1)
Enter fullscreen mode Exit fullscreen mode

Result: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Top comments (0)