Categories
JavaScript Answers

How to declare array of objects with JavaScript?

Spread the love

Sometimes, we want to declare array of objects with JavaScript.

In this article, we’ll look at how to declare array of objects with JavaScript.

How to declare array of objects with JavaScript?

To declare array of objects with JavaScript, we can use the array fill method.

For instance, we write

const arr = new Array(5).fill({ test: "a" });

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

Then we call fill with each slot with the { test: "a" } object.

Conclusion

To declare array of objects with JavaScript, we can use the array fill method.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.