Create an Array containing 1 to n numbers in JavaScript

In this tutorial, we will see how to create an array containing 1 to n numbers in JavaScript.

Arrays are really important in JavaScript, they let us store and work with a bunch of values. Sometimes, we need to make an array that holds numbers from 1 to a certain number, which we will call “n“.

Here we will see the different methods below to achieve this task.

Using for loop

We can create an array that contains numbers from 1 to n using a for loop. This is one of the most straightforward ways. The loop will iterate from 1 to n, pushing each number into an array.

Example

// Declare a variable 'n' and set it to 10
var n = 8;

// Declare an empty array 'myArray' to store the numbers
var myArray = [];

// Use a for loop to iterate from 1 to 'n'
for (var i = 1; i <= n; i++) {
  // Within the loop, push the current value of 'i' into 'myArray'
  myArray.push(i);
}

// Print the final array to the console
console.log(myArray);

Output:

[
1, 2, 3, 4,
5, 6, 7, 8
]

Using the Array.from() method

We can use the Array.form() method to create an array of numbers from 1 to n in JavaScript. This method creates a new array based on a specified length and a mapping function.

Example

const n = 12;
const numbers = Array.from({ length: n }, (_, index) => index + 1);

console.log(numbers);

Output:

[
1, 2, 3, 4, 5,
6, 7, 8, 9, 10,
11, 12
]

Using Array.keys() method

We can use the Array.keys() method along with the Array.from() method to create an array of numbers from 1 to n. The Array.keys() function returns an iterator for generating an array’s keys.

Example

const n = 10;
const numbers = Array.from(Array(n).keys(), x => x + 1);

console.log(numbers);

Output:

[
1, 2, 3, 4, 5,
6, 7, 8, 9, 10
]

Using Spread Operator

We can also use the spread operator (...) along with the Array.from() method to create an array of numbers from 1 to n.

Example

const n = 7; // Set the desired length of the array

// Create an array of numbers from 1 to n using the spread operator
const numbers = [...Array(n).keys()].map(index => index + 1);

console.log(numbers); // Output the resulting array

Output:

[
1, 2, 3, 4,
5, 6, 7
]

Using third-party library

We can use here the lodash library, the lodash library is a popular JavaScript utility library that provides many useful functions for working with arrays, objects, strings, and more.

This library provides a function called _.range(), which we can use to generate an array of numbers within a specified range.

First of all, we need to install the lodash library to use the _.range() function. We can do this using npm (Node Package Manager) by running the following commands in our terminal or command prompt.

npm init -y
npm install lodash

Now, we can require the lodash library in our JavaScript code and use the _.range() method to create an array of numbers.

Example

// Requiring the lodash library
const _ = require("lodash");

// Using the _.range() method to create an array of numbers from 1 to 7
let range_arr = _.range(1, 8);

// Printing the output
console.log(range_arr);

Output:

[
1, 2, 3, 4,
5, 6, 7
]

Leave a Reply

Your email address will not be published. Required fields are marked *