Skip to main content

Javascript equivalent of python range function

For ES6

const len = 10
const start = 99
Array(len).fill().map((_, i) => i + start)

will output

[ 99, 100, 101, 102, 103, 104, 105, 106, 107, 108 ]

For older versions of Javascript:

let size = 10, startAt = 1
[...Array(size).keys()].map(i => i + startAt)

produces

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

or, simply for a range starting at zero

[...Array(9).keys()]

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

See this posting for a more complete discussion.