AshKeys

Confessions and Confusions of a Freelance Fullstacker.

Ashok Mannolu Arunachalam, How ToArrayJS
Back

How to fill an array of N numbers in JavaScript

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.

js
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.

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