Skip to main content

range()

range(start, end?, step?): number[]

Creates an array of numbers from start up to, but not including, end.

note

Automatically determines direction when step is omitted.


Parametersโ€‹

start: numberโ€‹

The start of the range (or end if end is omitted, starting from 0).

end?: numberโ€‹

The end of the range (exclusive).

step?: numberโ€‹

The increment/decrement value. Defaults to 1 or -1 based on direction.


Returns: number[]โ€‹

The range of numbers.


Throwsโ€‹

RangeError If step is zero.


Sinceโ€‹

2.0.0


Performanceโ€‹

O(n) time & space where n is range size. Uses for loop with pre-allocated array. Separate loops for positive/negative steps.


Also known asโ€‹

range (Lodash, es-toolkit, Remeda, Radashi, Ramda, Effect, Modern Dash, Antfu)


Exampleโ€‹

range(5);        // => [0, 1, 2, 3, 4]
range(0, 10, 2); // => [0, 2, 4, 6, 8]
range(5, 0); // => [5, 4, 3, 2, 1]
range(5, 0, -2); // => [5, 3, 1]

How it works?โ€‹

Creates an array of numbers from start up to (but not including) end.

Signaturesโ€‹

CallResult
range(5)[0, 1, 2, 3, 4]
range(0, 10, 2)[0, 2, 4, 6, 8]
range(5, 0)[5, 4, 3, 2, 1]
range(5, 0, -2)[5, 3, 1]

Direction Detectionโ€‹

Ascending vs Descendingโ€‹


Use Casesโ€‹

Generate numeric sequences ๐Ÿ“Œโ€‹

Create arrays of numbers for iteration or data generation. Comparable to Python's range() function.

const indices = range(5); // [0, 1, 2, 3, 4]
const evens = range(0, 10, 2); // [0, 2, 4, 6, 8]

Create pagination controlsโ€‹

Generate page numbers for navigation components.

const totalPages = 10;
const pages = range(1, totalPages + 1); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Render placeholder skeletonsโ€‹

Generate indices for list rendering in React.

const skeletons = range(pageSize).map((i) => (
<Skeleton key={i} />
));

Generate dropdown options for formsโ€‹

Create numeric options for time pickers, year selectors, or quantity inputs. Very common in form-heavy applications with numeric selection fields.

// Hour picker: 0โ€“23
const hours = range(0, 24);
// => [0, 1, 2, ..., 23]

// Year selector: 2020โ€“2030
const years = range(2020, 2031);
// => [2020, 2021, ..., 2030]

// Quantity selector: 1โ€“10
const quantities = range(1, 11);
// => [1, 2, 3, ..., 10]

// Render as <select> options
years.map((year) => <option key={year} value={year}>{year}</option>);