Categories
JavaScript Answers

How to easily create empty matrices with JavaScript?

Spread the love

Sometimes, we want to easily create empty matrices with JavaScript.

In this article, we’ll look at how to easily create empty matrices with JavaScript.

How to easily create empty matrices with JavaScript?

To easily create empty matrices with JavaScript, we can use the array fill method.

For instance, we write

const matrix = Array(9).fill(Array(9));

to create an array with length 9 with the Array function.

Then we call fill with another empty array with 9 empty entries.

As a result, we get a 9×9 2d array returned.

Conclusion

To easily create empty matrices 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.