JS: Range Function for Array

By Xah Lee. Date: . Last updated: .

Range Function for Array, ES2015

const xah_range = ((n) => [...Array(n).keys()] ) ;

console.log ( xah_range(4) ); // [ 0, 1, 2, 3 ]
const xah_range = ((min, max , step = 1) => {
    // return a array from min to max, inclusive, in steps of step.
    // if step is not integer, then max may not be included
    // version 2017-04-20
    const arr = [];
    const totalSteps = Math.floor((max - min)/step);
    for (let ii = 0; ii <= totalSteps; ii++ ) { arr.push(ii * step + min) }
    return arr;
} );

console.log( xah_range(3, 7) ); // [ 3, 4, 5, 6, 7]

console.log( xah_range(3, 4.5) ); // [ 3, 4]
console.log( xah_range(3.2, 4.5) ); // [ 3.2, 4.2]

console.log( xah_range(3, 5, .25) ); // [ 3, 3.25, 3.5, 3.75, 4, 4.25, 4.5, 4.75, 5 ]

console.log( xah_range(5, 3, -.25) ); // [ 5, 4.75, 4.5, 4.25, 4, 3.75, 3.5, 3.25, 3 ]

console.log( xah_range(3, 5, .7) ); // [ 3, 3.7, 4.4]

ES5

There's no range function to creat a array.

The fastest solution is to simply use a for loop.

Here's starting with 0.

function xah_range0 (n) {
    // create a array, from 0 to n, including n
    var m = n+1;
    var arr = Array(m);
    for (var ii = 0; ii < m; ii++) { arr[ii] = ii; };
    return arr;
}

console.log( xah_range0(3) ); // [ 0, 1, 2, 3]

Here's starting with 1.

function xah_range1 (n) {
    // create a array, from 1 to n, including n

    var arr = Array(n);
    for (var ii = 0; ii < n; ii++) { arr[ii] = ii+1; };
    return arr;
}

console.log( xah_range1(4) ); // [ 1, 2, 3, 4]

Here's a generalized range function, that allows {start, end, step} with end and step as optional.

function xah_range (min, max, delta) {
    // create array
    // xah_range(n) creates a array from 1 to n, including n.
    // xah_range(m,n) creates a array from m to n, including n.
    // xah_range(n,m,delta) creates a array from n to m, by step of delta. May not include m
    // version 2016-10-22 http://xahlee.info/js/javascript_range_array.html

    var arr = [];
    var myStepCount;

    if ( arguments.length === 1 ) {
        for (var ii = 0; ii < min; ii++) {
            arr[ii] = ii+1;
        };
    } else {
        if ( arguments.length === 2 ) {
            myStepCount = (max - min);
            for (var ii = 0; ii <= myStepCount; ii++ ) {
                arr.push(ii + min);
            };
        } else {
            myStepCount = Math.floor((max - min)/delta);
            for (var ii = 0; ii <= myStepCount; ii++ ) {
                arr.push(ii * delta + min);
            };
        }
    }

    return arr;

}

console.log( xah_range(4) ); // [ 1, 2, 3, 4]

console.log( xah_range(3, 7) ); // [ 3, 4, 5, 6, 7]

console.log( xah_range(3, 4.5) ); // [ 3, 4]
console.log( xah_range(3.2, 4.5) ); // [ 3.2, 4.2]

console.log( xah_range(3, 5, .25) ); // [ 3, 3.25, 3.5, 3.75, 4, 4.25, 4.5, 4.75, 5 ]

console.log( xah_range(5, 3, -.25) ); // [ 5, 4.75, 4.5, 4.25, 4, 3.75, 3.5, 3.25, 3 ]

console.log( xah_range(3, 5, .7) ); // [ 3, 3.7, 4.4]

Array Topic

  1. JS: Array Basics
  2. JS: Understand JS Array
  3. JS: Create Array
  4. JS: Sparse Array
  5. JS: Array-Like Object
  6. JS: Array How-To

  1. JS: Array Object
  2. JS: Array.prototype
Like what you read? Buy JavaScript in Depth