This post will discuss how to initialize an array with a range from 0 to N in JavaScript.

There are several ways to create a numbered integer array from 0 (inclusive) to N (exclusive), incremented by step 1, where the value N is dynamic.

1. Using ES6 Spread operator

Download  Run Code

2. Using Underscore range() method

Download Code

3. Using Array.from() function

Download  Run Code

 
Alternatively, using Array.from() with Array.prototype.keys() method in ES6.

Download  Run Code

4. Using Array.prototype.map() function

Download  Run Code

5. Using Function.prototype.apply() function

Download  Run Code

6. Using array literal notation

Download  Run Code

7. Using Array Constructor

Download  Run Code

That’s all about initializing an array with a range of 0 to N in JavaScript.