range
export declare function range(range: RangeOptions | number): Generator<number, void, unknown>;
export declare function range(range: RangeOptions | number): Generator<number, void, unknown>;
A generator to yield numbers in a given range
Remarks
This method is end-exclusive, for example the last number yielded byrange(5)
is 4. If you prefer for the end to be included add 1 to the range or end
option.Example
Basic rangefor (const number of range(5)) {
console.log(number);
}
// Prints 0, 1, 2, 3, 4
for (const number of range(5)) {
console.log(number);
}
// Prints 0, 1, 2, 3, 4
Example
Range with a stepfor (const number of range({ start: 3, end: 10, step: 2 })) {
console.log(number);
}
// Prints 3, 5, 7, 9
for (const number of range({ start: 3, end: 10, step: 2 })) {
console.log(number);
}
// Prints 3, 5, 7, 9
Name | Type | Optional | Description |
---|---|---|---|
range | RangeOptions | number | No | A number representing the the range to yield (exclusive) or an object with start, end and step |