Categories
JavaScript Answers

How to create empty array of a given size with JavaScript?

Spread the love

Sometimes, we want to create empty array of a given size with JavaScript.

In this article, we’ll look at how to create empty array of a given size with JavaScript.

How to create empty array of a given size with JavaScript?

To create empty array of a given size with JavaScript, we use the Array constructor.

For instance, we write

const arr = Array(5);
console.log(arr.length);

to call Array with 5 to return an array with 5 empty slots.

Conclusion

To create empty array of a given size with JavaScript, we 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 *