A proposal for ECMAScript to add a built-in Iterator.range()
528
stars
90
commits
HTML
primary language
Feb 5, 2025
updated
Champions: Jack Works
Author: Jack Works
Stage: 2
This proposal describes adding Iterator.range to JavaScript.
See the rendered spec at here.
Compare with other languages (Work in progress)
A polyfill is available in the core-js library. You can find it in the ECMAScript proposals section.
In the proposal repo a step-by-step implementation of the proposal is available so you can verify if there is a bug in the specification using the debugger.
Without these functions, the language feels incomplete, and is a paper cut to what could be a very polished experience. Bringing this into the platform will improve performance of the web, and developer productivity as they no longer have to implement these common functions.
—— String.prototype.{padStart,padEnd}
range is a very useful function. For example in Python:
for i in range(5):
# ...
At least 20 different implementations in a single stackoverflow question.
Tons of libraries providing a range: math.js, lodash, underscore.js, ramda, d3, range, fill-range, multi-integer-range, ……
Iterator.range(0, Infinity) -> (0, 1, 2, 3, ...)if (x in Iterator.range(0, 10)) (Kotlin has this feature)See tests to learn about more usages.
for (const i of Iterator.range(0n, 43n)) console.log(i) // 0n to 42n
Iterator.range(0, Infinity)
.take(1000)
.filter((x) => !(x % 3))
.toArray()
function* even() {
for (const i of Iterator.range(0, Infinity)) if (i % 2 === 0) yield i
}
;[...Iterator.range(1, 100, 2)] // odd number from 1 to 99
See global.d.ts
HTML
96.5%
JavaScript
3.5%
A proposal for ECMAScript to add a built-in Iterator.range()
528
stars
90
commits
HTML
primary language
Feb 5, 2025
updated
Champions: Jack Works
Author: Jack Works
Stage: 2
This proposal describes adding Iterator.range to JavaScript.
See the rendered spec at here.
Compare with other languages (Work in progress)
A polyfill is available in the core-js library. You can find it in the ECMAScript proposals section.
In the proposal repo a step-by-step implementation of the proposal is available so you can verify if there is a bug in the specification using the debugger.
Without these functions, the language feels incomplete, and is a paper cut to what could be a very polished experience. Bringing this into the platform will improve performance of the web, and developer productivity as they no longer have to implement these common functions.
—— String.prototype.{padStart,padEnd}
range is a very useful function. For example in Python:
for i in range(5):
# ...
At least 20 different implementations in a single stackoverflow question.
Tons of libraries providing a range: math.js, lodash, underscore.js, ramda, d3, range, fill-range, multi-integer-range, ……
Iterator.range(0, Infinity) -> (0, 1, 2, 3, ...)if (x in Iterator.range(0, 10)) (Kotlin has this feature)See tests to learn about more usages.
for (const i of Iterator.range(0n, 43n)) console.log(i) // 0n to 42n
Iterator.range(0, Infinity)
.take(1000)
.filter((x) => !(x % 3))
.toArray()
function* even() {
for (const i of Iterator.range(0, Infinity)) if (i % 2 === 0) yield i
}
;[...Iterator.range(1, 100, 2)] // odd number from 1 to 99
See global.d.ts
HTML
96.5%
JavaScript
3.5%