Creating an array of a given size can be implemented in many ways, and each way is useful in some conditions. We will explain each possible ways to create an array with the given size with a proper example.
Below are the following ways:
- Using JavaScript array() constructor
- Using apply() and map() Methods
- JavaScript Array.from() Method
- Using for loop
Method 1: Using JavaScript array() constructor
In this method, we use the JavaScript Array constructor to create an array. It will just create an array without any value with a given size. But the values of the array will be undefined.
Example:
Javascript
let arr = new Array(5); console.log(arr.length); console.log(arr) |
5 [ <5 empty items> ]
Method 2: Using apply() and map() Methods
We will use apply() and map() methods to create an array with the given size. It will create an array without any value with a given size. But the values of the array will be undefined.
Example 1:
Javascript
let arr = Array.apply( null , Array(5)) .map( function () { }); console.log(arr.length); console.log(arr); |
Output:
5
[undefined, undefined, undefined, undefined, undefined]
Example 2: We will put the values of the indexes so it will give you an array of length 5 and its values will be the index number 0, 1, 2, 3, 4.
Javascript
let arr = Array.apply( null , Array(5)) .map( function (y, i) { return i; }); console.log(arr.length); console.log(arr) |
Output:
5
[0, 1, 2, 3, 4]
Method 3: Using JavaScript Array.from() Method
In ES6, we got something new called JavaScript Array.from() function, by which we can create an array of a given size.
Example:
Javascript
let arr = Array.from(Array(5)); console.log(arr.length); console.log(arr); |
Output:
5
[undefined, undefined, undefined, undefined, undefined]
Example 2: In this example, we will pass the elements as arguments, each character will be an array element
Javascript
let arr = Array.from( 'GEEKSFORGEEKS' ); console.log(arr.length); console.log(arr) |
Output:
13
["G", "E", "E", "K", "S", "F", "O", "R", "G", "E", "E", "K", "S"]
Example 3: In this example, we will pass a single element as a parameter and repeat the length we want in our array.
Javascript
let arr = Array.from( 'G' .repeat(5)); console.log(arr.length); console.log(arr); |
Output:
5
["G", "G", "G", "G", "G"]
Example 4: In this method, we will use two features of ES6, one JavaScript Array.from() function and the arrow function to create an array with a given size.
Javascript
let arr = Array.from({ length: 5 }, (x, i) => i); console.log(arr.length); console.log(arr) |
Output:
5
[0, 1, 2, 3, 4]
Method 4: Using for loop
Looping in programming languages is a feature that facilitates the execution of a set of instructions repeatedly until some condition evaluates and becomes false.
Example:
Javascript
const size = 5; const array = []; for (let i = 0; i < size; i++) { array.push(undefined); } console.log(array); |
[ undefined, undefined, undefined, undefined, undefined ]
To more about Arrays in JavaScript please go through Arrays in JavaScript