Range in javascript / jQuery?

Possible duplicate:
Does JavaScript have a range () equivalent?

Is there a way to declare a range in Javascript / jQuery, like in Python?

Something like that:

x = range (1.10)

x = [1,2,3,4,5,6,7,8,9]

Thanks.

+7
source share
3 answers

Using some third-party libraries, such as Underscore.js, you can achieve the same behavior as in Python http://underscorejs.org/#range

+4
source

You can simply create an array, iterate over the values ​​using the for loop and push values. There is nothing built into the language.

+3
source

Put this function in your Javascript code, and you should be able to call range() , as in Python (but it only works for numbers):

 function range(start, end) { var array = new Array(); for(var i = start; i < end; i++) { array.push(i); } return array; } 
+1
source

All Articles