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.
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.
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:
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.
Follow the mentioned syntax for the Array.from() method to create an array containing numbers 1 to N:
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:
(item, index) => item = index + 1
Lastly, print the resultant array returned from the Array.from() method:
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.
For creating an array using the spread operator, use the given syntax:
Here, “Array(N)” will create an array of N empty elements.
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.
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.