Skip to main content Accessibility Feedback


Create a range of numbers between two values.

Source Code


// returns [1,2,3,4,5,6,7]
let first = range(7);

// returns [4,5,6,7,8,9,10,11,12]
let second = range(4, 12);

// returns [-12,-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7]
let third = range(-12, 7);

The helper function

 * Create a range of numbers
 * Adapted with permission from Darren Jones,
 * (c) Chris Ferdinandi, MIT License,
 * @param  {Number} min The first number in the range
 * @param  {Number} max The last number in the range
 * @return {Array}      The range
function range (min, max) {

	// If only one number is provided, start at one
	if (max === undefined) {
		max = min;
		min = 1;

	// Create a ranged array
	return Array.from(new Array(max - min + 1).keys()).map(function (num) {
		return num + min;


Preorder my new course on Web Components! Want to learn how to build Web Components from scratch, master best practices, and more? Preorder today and get $100 off of the launch price.

Find this useful? You can support my work by purchasing an annual membership.