Skip to content
Home » How to Generate an Array with Incrementing Values using JavaScript?

How to Generate an Array with Incrementing Values using JavaScript?

In JavaScript, there are multiple ways to generate an array with incrementing or decrementing values. In this blog post, we will explore different code snippets that demonstrate how to generate an array with incrementing values. These techniques can be useful in various scenarios such as generating number sequences or initializing arrays with specific values.

Code

[...Array(10)].map((_, i) => 4 + i * 2);
//=> [4, 6, 8, 10, 12, 14, 16, 18, 20, 22]

Array.from(Array(10)).map((_, i) => 4 + i * 2);
//=> [4, 6, 8, 10, 12, 14, 16, 18, 20, 22]

Array.from(Array(10).keys()).map(i => 4 + i * 2);
//=> [4, 6, 8, 10, 12, 14, 16, 18, 20, 22]

[...Array(10).keys()].map(i => 4 + i * -2);
//=> [4, 2, 0, -2, -4, -6, -8, -10, -12, -14]

Array(10).fill(0).map((_, i) => 4 + i * 2);
//=> [4, 6, 8, 10, 12, 14, 16, 18, 20, 22]

Array(10).fill().map((_, i) => 4 + i * -2);
//=> [4, 2, 0, -2, -4, -6, -8, -10, -12, -14]

Code Explanation

1. Using the spread operator and map function:

JavaScript
   [...Array(10)].map((_, i) => 4 + i * 2);
   //=> [4, 6, 8, 10, 12, 14, 16, 18, 20, 22]

In this code snippet, the spread operator ([...Array(10)]) is used to create an array of length 10. The map function is then used to apply the desired formula to each element of the array. In this case, we add 4 to each element’s index multiplied by 2 to generate the incrementing values.

2. Using Array.from and map function:

JavaScript
   Array.from(Array(10)).map((_, i) => 4 + i * 2);
   //=> [4, 6, 8, 10, 12, 14, 16, 18, 20, 22]

Here, the Array.from method is used to create an array of length 10. The map function is then applied to add 4 to each element’s index multiplied by 2, resulting in the desired incrementing values.

3. Using Array.from and keys:

JavaScript
   Array.from(Array(10).keys()).map(i => 4 + i * 2);
   //=> [4, 6, 8, 10, 12, 14, 16, 18, 20, 22]

This snippet also utilizes the Array.from method along with the keys iterator to generate an array of length 10. The map function is used to add 4 to each element’s index multiplied by 2, producing the desired incrementing values.

4. Using spread operator and keys:

JavaScript
   [...Array(10).keys()].map(i => 4 + i * -2);
   //=> [4, 2, 0, -2, -4, -6, -8, -10, -12, -14]

In this code snippet, we combine the spread operator and the keys iterator to create an array of length 10. The map function is then used to add 4 to each element’s index multiplied by -2, resulting in a decrementing sequence of values.

5. Using Array.fill and map function:

JavaScript
   Array(10).fill(0).map((_, i) => 4 + i * 2);
   //=> [4, 6, 8, 10, 12, 14, 16, 18, 20, 22]

Here, the Array.fill method is used to initialize an array of length 10 with the value 0. The map function is then applied to add 4 to each element’s index multiplied by 2, generating the desired incrementing values.

6. Using Array.fill and map function with negative values:

JavaScript
   Array(10).fill().map((_, i) => 4 + i * -2);
   //=> [4, 2, 0, -2, -4, -6, -8, -10, -12, -14]

Similar to the previous snippet, the Array.fill method is used to initialize an array of length 10. The map function is applied to add 4 to each element’s index multiplied by -2, generating a decrementing sequence of values.

These code snippets provide different ways to generate arrays with incrementing or decrementing values in JavaScript. Depending on your specific use case, you can choose the method that best suits your needs. Experiment with these techniques and be creative in using them to solve various programming challenges. Happy coding!

Remember, it’s important to understand the logic behind these code snippets and not just blindly copy them. Understanding how the code works will enable you to modify and adapt it to different scenarios.

Also checkout the following codes.


Program to Generate a Range of Numbers in JavaScript
How to Use the Range Class to Generate Sequential Elements in JavaScript