Categories
JavaScript Answers

How to create an array of empty strings with JavaScript?

Spread the love

Sometimes, we want to create an array of empty strings with JavaScript.

In this article, we’ll look at how to create an array of empty strings with JavaScript.

How to create an array of empty strings with JavaScript?

To create an array of empty strings with JavaScript, we can use the Array function and the fill method.

For instance, we write:

const arr = Array(20).fill("");
console.log(arr)

to create an array with 20 empty slots with Array(20).

Then we call fill with an empty string to fill the empty slots with empty strings.

Therefore, arr is an array with 20 empty strings.

Conclusion

To create an array of empty strings with JavaScript, we can use the Array function and the fill method.

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 *