Sometimes, we want to create an empty array of a given size with JavaScript.
In this article, we’ll look at how to create an empty array of a given size with JavaScript.
Create an Empty Array of a Given Size with JavaScript
To create an empty array of a given size with JavaScript, we can use the Array
constructor.
For instance, we can write:
const arr = new Array(5);
console.log(arr.length)
to create an empty array with length 5 by passing in the length as the argument for the Array
constructor.
Therefore, arr.length
is 5 according to the console log.
Conclusion
To create an empty array of a given size with JavaScript, we can use the Array
constructor.