Let’s say we want to create a list of numbers from [0..9].

A simple approach would be to use a for loop

const numbers = []

for (let i = 0; i < 10; i++) {
  numbers.push(i)
}

// numbers => [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

Another approach is to use the Array.prototype.keys() method which returns the index of each element

const numbers = [...Array(10).keys()]

// numbers => [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

Perhaps the most elegant solution is to use Array.from()

const numbers = Array.from({ length: 10 }, (_, i) => i)

// numbers => [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

Array.from() takes an array-like object as its first argument and a map function that will be applied to each element. In this case, we pass an object with a length property and then we take just return the index from the map function.

If we wanted to start our list at 1 instead, then we can do this

Array.from({ length: 10 }, (_, i) => i + 1)

// numbers => [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]

Or, more generally, if we wanted include 10 numbers starting from a certain number

const startingNumber = 3

Array.from({length: 10}, (_, i) => i + startingNumber)

// numbers => [ 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ]

References