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.