Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create simple array "[x..y]" with JavaScript [duplicate]

Tags:

javascript

I want to design some arrays like [1..25] in JavaScript, but I don't want to do by hand ([1, 2, 3, 4, 5, 6, [...], 25]).

How would you do?

like image 573
Guilherme Oderdenge Avatar asked Dec 21 '25 02:12

Guilherme Oderdenge


1 Answers

Well you could make a simple function...

function range(min, max) {
  var len = max - min + 1;
  var arr = new Array(len);
  for (var i=0; i<len; i++) {
    arr[i] = min + i;
  }
  return arr;
}

range(1,10);
// [1,2,3,4,5,6,7,8,9,10]

This answer is not the smallest amount of code, but it's very readable and tremendously faster than any other solution provided here.

like image 70
maček Avatar answered Dec 22 '25 16:12

maček



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!