Skip to content

range

Generates an array of sequential numbers from start to end (exclusive). If only one argument is provided, it generates numbers from 0 to that value.

Available since v2.0.0

import { range } from '@helpers4/array';
range(startOrEnd: number, end?: number, step?: number): number[]
ParameterTypeDescription
startOrEndnumberThe start value (if end is provided) or end value (if end is omitted)
endnumberThe end value (exclusive) (optional)
stepnumberThe increment between values (default: 1 or -1 depending on direction) (optional)

number[] — An array of sequential numbers

Creates an array of numbers from 0 to n-1 with a single argument.

range(5)
// => [0, 1, 2, 3, 4]

Creates an array from start (inclusive) to end (exclusive).

range(1, 5)
// => [1, 2, 3, 4]

Creates an array with a specified increment between values.

range(0, 10, 2)
// => [0, 2, 4, 6, 8]

Automatically produces a descending range when start > end.

range(5, 0)
// => [5, 4, 3, 2, 1]

View source on GitHub