How to create an array of numbers in javascript

This tutorial covers multiple ways we can create an array with a sequence of numbers.

Using Array literal syntax

Array variables are defined by assigning numbers using literal syntax.

Literal Syntax contains elements, separated by a comma in Square Brackets [].

const numbers = [0,1,2,6,1];
console.log(numbers);

Notes:

  • Easy, simple way to create numbers with fixed values
  • Not suitable for a large number of elements or need processed values

Use Array constructor

Array constructors are used to creating an array variable with fixed set values, and inline assignment.

const numbers = Array(0,1,2,6,1);
console.log(numbers);

Notes:

  • Suitable for a finite number of elements and easy to declare, It improves readability
  • Literal syntax is more suitable than constructor, but not suitable for a larger array of elements.

Use Array.of function

Another way to create an array with numbers using the Array.of() function.

const numbers = Array.of(0,1,2,6,1);
console.log(numbers);

Notes:

  • It works with limited numbers, not support for process of numbers
  • Not recommended for a larger array of elements.

Using for loop

Using for loop with index assigned with initial number and increment by 1 until end of the number, and add an index to an array.

Here is an example to create an array of sequence numbers from 1 to 50.

var numbers = [];
for (var i = 1; i <= 50; i++) {
  numbers.push(i);
}
console.log(numbers);

Output:

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

Notes:

  • The developer has full control over what number to insert
  • Native way of writing using for loop and more code required

using the ES6 spread operator

es6 provides spread operator syntax to include an array with keys as an array of numbers.

The generated sequence of numbers using the spread operator and keys method in es6

var array = [...Array(10).keys()];
console.log(array);

output:

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

Use Arrays.from() function

Also, you can use Arrays.from() function in place of the spread operator

var array = Array.from(Array(10).keys());
console.log(array);

The above two examples generate numbers from zero.

if you want to generate from 1, Here is a code. It uses an arrow function with increment values by 1.

var array = Array.from(Array(10).keys(), (n) => n + 1);
console.log(array);

Output:

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

Using Array fill function

Sometimes, the fill() function is used to prefill an array with the given value.

Array size given to Array() constructor

const number = new Array(5).fill(5)
console.log(number); // [5,5,5,5,5]

Notes:

  • Used to fill with values using custom processing numbers
  • Can be used for larger data sets.

lodash range function

if the application uses the lodash library, It provides a range function that iterates with a range of min and max values. Syntax:

_.range([start], stop, [step]);

Here is an

start: It is a starting number or initial number, Stop: is the maximum value that needs to increment too. Step: needs value which increments by how much value, Default is 1.

_.range(1, 10);

Output:

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

Notes:

  • useful for projects already using this library, Code changes are not required, just a calling method.
  • Separate library needs installation

Underscore times function

times function is used to iterate the loop a given number of times and each iteration calls the callback function.

_.times(numberoftimes, callbackfunction);

number: It iterates multiple times with the given call-back function. callbackfunction: This function called for each iteration

array = [];
_.times(5, function (i) {
  array.push(i++);
});

Output:

[1, 2, 3, 4, 5]

Notes:

  • Suitable for applications, already using the library, only usage of method is required in code
  • Separate library installation required, need to import it.

Conclusion

Learn multiple ways to create an array of sequence numbers for a given min and max values in javascript.