Sometime we want to fill an array with a range of numbers like 1 to 10. Let’s how can we do that in JavaScript.

1
2
3
4
5
6
7
8
Array.from({ length: 10 }).map((_, i) => ++i);

/*
[
1, 2, 3, 4, 5,
6, 7, 8, 9, 10
]
*/

In the above snippet, we are first creating an array from an iterable with length of 10 (our range). Then we map to a new value that is being generated by increamenting the index value.

Note: An iterable is an object with a length property and indexed elements.