How to use array() constructor In Javascript
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. However, the values of the array will be undefined.
Example: In this example, we are using an array constructor for the formation of an array.
let arr = new Array(5);
console.log(arr.length);
console.log(arr)
Output
5 [ <5 empty items> ]
Create an array of given size in JavaScript
Creating an array of a specific size can be achieved through various methods, each suited to different situations. In this article, we will explore several approaches to creating arrays of a desired size, accompanied by examples for each method.
Below are the following ways:
Table of Content
- Using JavaScript array() constructor
- Using apply() and map() Methods
- Using JavaScript Array.from() Method
- Using for loop
- Using the spread syntax with Array.fill()