Create array of all integers between two numbers, inclusive, in Javascript/jQuery
var list = [];
for (var i = lowEnd; i <= highEnd; i++) {
list.push(i);
}
In JavaScript ES6:
function range(start, end) {
return Array(end - start + 1).fill().map((_, idx) => start + idx)
}
var result = range(9, 18); // [9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
console.log(result);
For completeness, here it is with an optional step
parameter.
function range(start, end, step = 1) {
const len = Math.floor((end - start) / step) + 1
return Array(len).fill().map((_, idx) => start + (idx * step))
}
var result = range(9, 18, 0.83);
console.log(result);
I would use range-inclusive
from npm in an actual project. It even supports backwards steps, so that's cool.
ES6 :
Use Array.from (docs here):
console.log(
Array.from({length:5},(v,k)=>k+1)
)