Wednesday, July 3, 2024
HomeLanguagesJavascriptCreate an array of given size in JavaScript

Create an array of given size in JavaScript

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:

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)


Output

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);


Output

[ undefined, undefined, undefined, undefined, undefined ]

To more about Arrays in JavaScript please go through Arrays in JavaScript

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments