Sometimes, we want to initialize a boolean array in JavaScript.
In this article, we’ll look at how to initialize a boolean array in JavaScript.
How to initialize a boolean array in JavaScript?
To initialize a boolean array in JavaScript, we can use the array fill
method.
For instance, we write:
const arr = new Array(10).fill(false);
console.log(arr)
to create the array with Array
with length 10.
Then we call fill
with false
to fill all 10 slots with false
.
Therefore, arr
is [false, false, false, false, false, false, false, false, false, false]
.
Conclusion
To initialize a boolean array in JavaScript, we can use the array fill
method.