JavaScript: Range Function for Array

By Xah Lee. Date: . Last updated: .

There's no builtin range function to creat a array.

Here is a general range function.

/* [
xah_range(min, max) β†’ return a array from min to max, inclusive.
xah_range(min, max, step) β†’ in steps of step.
If step is not integer, then max may not be included.
http://xahlee.info/js/javascript_range_array.html
version 2019-10-31
] */
const xah_range = (( min, max, step = 1, ) => (Array(Math.floor((max - min) / step) + 1).fill(min).map( ((x, i) => (x + i * step)), )));

back to Array How-To

JavaScript Array

βˆ‘ JS in Depth
XAH Β BUY NOW

JS in Depth

JS Obj Ref

DOM


JS in Depth

Basic Syntax

Value Types

Variable

String

Function

Property

Object and Inheritance

Array

Constructor/Class

Iterable 🌟

Misc