javascript create an array from 1 to n code example
Example 1: create an array from 1 to n javascript
Array.from(Array(10).keys())
[...Array(10).keys()]
Array.from({length: 10}, (_, i) => i + 1)
Example 2: js array of 1 to n
[...Array(10).keys()]
Example 3: es6 create array with increasing number
Array.from(Array(10).keys())
Example 4: javascript fill array with range
function range(start, end) {
return Array(end - start + 1).fill().map((_, idx) => start + idx)
}
var result = range(9, 18);
console.log(result);
Example 5: generate array range
for (i of range(1, 5)) {
console.log(i);
}
[...range(1, 5)]
Example 6: javascript create array from 1 to n
[...Array(n+1).keys()].slice(1)