Create array of numbers in range JavaScript [SOLVED]


JavaScript

Introduction

Unlike other programming languages like Python, JavaScript doesn’t have a built-in function that can create array of numbers within a range. Say, we want to have a series or sequence of numbers from 2 to 23, it will be counter-intuitive to write each number.

In JavaScript, we have a couple of ways to create array of numbers in range. In this article, we will discuss the different approaches to solving the problem.

 

Method-1: Use Array.from() method to create array of numbers in range

Array.from is another array factory method introduced in ES6. It expects an iterable or array-like object as its first argument and returns a new array that contains the elements of that object. With an iterable argument, Array.from(iterable) works like the spread operator [...iterable] does. It is also a simple way to make a copy of an array:

let copy = Array.from(original);

Array.from() also accepts an optional second argument. If you pass a function as the second argument, then as the new array is being built, each element from the source object will be passed to the function you specify, and the return value of the function will be stored in the array instead of the original value.

To create an array of numbers within a specific range in JavaScript, you can use the Array.from() method. Here's an example of how this can be done:

const numbers = Array.from({ length: 10 }, (_, i) => i + 1);
console.log(numbers);

Output

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

In the example above, we used the Array.from() method to create an array with a length of 10. This creates an array with 10 empty elements, which we then use the map() method to transform into an array of numbers from 1 to 10. The map() method takes a callback function as an argument, which is called for each element in the array.

In this case, we used an arrow function that takes two arguments: _, which is a placeholder for the element, and i, which is the index of the element. We use the i argument to create the numbers, starting from 1 and incrementing by 1 for each element in the array.

It's important to note that the Array.from() method is only supported in modern browsers, so if you need to support older browsers, you may need to use a different approach.

 

Method-2: Use Array() method to create array of numbers in range

We can create a sequence of elements using the Array constructor. For example, you could create an array with a length of 10 using the new Array() constructor, and then use a for loop to populate the array with numbers from 2 to 11. Here's an example of how this could be done:

const numbers = new Array(10);

for (let i = 0; i < numbers.length; i++) {
    numbers[i] = i + 1;
}

console.log(numbers);

Output

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

 

Summary

As you can see, creating an array of numbers within a specific range in JavaScript is a straightforward process that can be done using the Array.from() method along with the map() method, or using the new Array() constructor and a for loop. Either approach can be effective for creating an array of numbers within a specific range in JavaScript.

 

References

Array() constructor - JavaScript | MDN (mozilla.org)
Array.from() - JavaScript | MDN (mozilla.org)

 

Olorunfemi Akinlua

Olorunfemi Akinlua

He is boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs user-friendly interfaces to enhance digital experiences across various domains. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment