Does JavaScript have a method like "range()" to generate a range within the supplied bounds?

10    Asked by kieran_4036 in Java , Asked on Sep 14, 2025

Does JavaScript have a built-in range() method like Python, and how can you generate ranges within specific bounds?  While JavaScript doesn’t include a direct range() function, you can easily create ranges using loops, Array.from(), or custom helper functions.

Answered by Kaalappan shingh

JavaScript does not have a built-in range() function like Python, but you can still generate ranges easily using different approaches. A range is simply a sequence of numbers between a starting and ending point, often used in loops, iterations, or data processing.

Here are some common ways to create a range in [removed]

Using a for loop:

 let range = [];
for (let i = 1; i <= 5; i++) {
  range.push(i);
}
console.log(range); // [1, 2, 3, 4, 5]

 This is the simplest method and works in all JavaScript environments.

Using Array.from():

 let range = Array.from({ length: 5 }, (_, i) => i + 1);
console.log(range); // [1, 2, 3, 4, 5]

 Here, Array.from() creates an array of a specified length and fills it with values based on a mapping function.

Using ... spread and keys():

 let range = [...Array(5).keys()].map(i => i + 1);
console.log(range); // [1, 2, 3, 4, 5]

Custom helper function (most reusable):

 function range(start, end) {
  return Array.from({ length: end - start + 1 }, (_, i) => start + i);
}
console.log(range(3, 7)); // [3, 4, 5, 6, 7]

In summary:

  • JavaScript doesn’t provide range() out of the box.
  • You can generate ranges using loops, Array.from(), or helper functions.
  • A custom function is the cleanest approach if you need ranges often in your code.



Your Answer

Interviews

Parent Categories