Categories
JavaScript Answers

How to Create an Empty Array of a Given Size with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *