Creating an array of “N” numbers manually, such as 1000 or more, can be hectic and time-consuming. So, you can easily create any length of an array in JavaScript without typing manually. To do this, use the JavaScript built-in methods or approaches, like the “for” loop, “Array.from()” method or the “spread operator”.
This tutorial will demonstrate the methods to create an array of “N” numbers.
How to Create an Array Containing 1…N?
For creating an array that contains “n” numbers starting from 1, use the following methods:
- for loop
- from() method
- Spread operator
Method 1: Create an Array Containing 1 … N Using “for” Loop
Use the “for” loop with the “push()” method for creating an array of “N” numbers starting from any number such as “1”. It is the most commonly used method.
Syntax
Use the provided syntax of the “for” loop for creating an array:
//……
}
Let’s look at an example of creating an array using the “for” loop.
Example
First, create an empty array:
Create a variable “n”, which stores the length of an array. Here, we create an array of numbers 1 to 10:
Use the for() loop to print “n” numbers by assigning values to an empty array using the “push()” method:
array.push(i);
}
To begin with a number other than one, simply change the initial value of the variable “i”.
Finally, print the array on the console:
It can be observed that the array containing numbers 1 to N has been successfully created:
Method 2: Create an Array Containing 1 … N Using “Array.from()” Method
You can also utilize the “Array.from()” method to create an array with 1 to N numbers in it. It outputs an array from any object with a length property.
Syntax
Follow the mentioned syntax for the Array.from() method to create an array containing numbers 1 to N:
//….
})
Example
First, create a variable “array” that stores the resultant array by calling the “Array.from()” method. There is a prebuilt “map()” method in Array.from() method that iterates the elements and passes the current index and item to the function to create an array instance:
{length: 10},
(item, index) => item = index + 1
);
Lastly, print the resultant array returned from the Array.from() method:
Output
Method 3: Create an Array Containing 1 … N Using “Spread” Operator
Another way to create an array containing 1 to N is the “Spread operator” with the “keys()” and the “map()” methods. It first creates an array of N empty elements, and then the “keys()” method on the Array will return an iterator containing the keys (indices) of the elements in the array. The spread operator is then used to spread the keys into a new array, and the “map()” method is used to add 1 to each key, resulting in an array containing the numbers from 1 to N.
Syntax
For creating an array using the spread operator, use the given syntax:
Here, “Array(N)” will create an array of N empty elements.
Example
Use the spread operator with the Array constructor by passing “N=10” with keys() method:
It will print “N” numbers starting from 1 to 10:
We have gathered all the essential information relevant for creating an array containing 1 to N.
Conclusion
To create an array containing 1 to N, use the “for” loop with “push()” method, “Array.from()” method, or the “Spread operator” with the “key()” and the “map()” methods. In the first approach, the push() method is used with a for loop to add elements to an array. The Array.from() method has a pre-built map() method, while the third approach explicitly calls the map() method. In this tutorial, we demonstrated the methods for creating an array of “N” numbers.