How to Fill an Array with a Range of Numbers in JavaScript?

Estimated read time 1 min read

To fill an array with a range of numbers in JavaScript, you can use the Array.from method with a map function:

const start = 5;
const end = 15;
const arr = Array.from({length: end - start + 1}, (_, i) => i + start);
console.log(arr);

Alternatively, you can use a for loop:

const start = 5;
const end = 15;
const arr = [];
for (let i = start; i <= end; i++) {
  arr.push(i);
}
console.log(arr);

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply