Design & Download
Your JS Library
in Seconds!!!

Snippet: range() - Array of Numbers

Written by Christopher West (cwest) on December 04, 2015.
Creates an array of numbers within a given range.
function range(opt_start, stop, opt_step) {
  if (arguments.length < 2) {
    stop = opt_start;
    opt_start = 0;
  }
  for (var ret = [], t = (opt_step = opt_step || 1) > 0; t ? opt_start < stop : opt_start > stop; opt_start += opt_step) {
    ret.push(opt_start);
  }
  return ret;
}

range(...) API Documentation

Description

Create an array of numbers within a given range.

Parameters

  1. opt_start {number=}:
    Optional. Defaults to 0. The starting point of the range to return.
  2. stop {number}:
    The non-inclusive boundary of the range.
  3. opt_step {number=}:
    Optional. Defaults to 1. The difference between each subsequent number in the range.

Returns

Returns an array of all of the numbers in the range. If opt_step >= 0 but opt_start >= opt_stop, an empty array will be returned. Also, if opt_step <= 0, but opt_start <= opt_stop, an empty array will be returned.