Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to initialize a boolean array in JavaScript?
In JavaScript, boolean arrays are useful for storing true/false values. You can initialize them using several methods, each with different advantages depending on your needs.
We can initialize a boolean array in JavaScript in three ways:
Using the fill() Method
Using the Array.from() Method
Using the for Loop
Using the fill() Method
The fill() method is the simplest way to create a boolean array with all elements having the same value. It fills all array positions with a static value from start to end.
Syntax
// Initialize boolean array with fill() method let arrayName = new Array(array_length).fill(true/false);
Example
Here's how to initialize a boolean array with all true values using the fill() method:
<html>
<body>
<h3>Initializing a boolean array using fill() method</h3>
<p>Boolean array with all true values</p>
<button onclick="bool()">Click Here</button>
<div id="root"></div>
<script>
function bool() {
// Initializing an array named "myarray" with a length of 5
const myarray = new Array(5).fill(true);
document.getElementById('root').innerHTML = "[" + myarray + "]";
}
</script>
</body>
</html>
[true,true,true,true,true]
Using the Array.from() Method
The Array.from() method provides more flexibility by allowing you to define custom logic for each element. It creates an array from an iterable object or array-like structure.
Syntax
// Initialize boolean array with Array.from() method
let arrName = Array.from({length}, (value, index) => true/false);
Example
This example creates a boolean array with all false values using Array.from():
<html>
<body>
<h3>Initializing a boolean array using Array.from() method</h3>
<p>Boolean array with all false values</p>
<button onclick="bool()">Click Here</button>
<div id="root"></div>
<script>
function bool() {
// Initializing an array named "myarray" with a length of 5
const myarray = Array.from({ length: 5 }, (value, index) => false);
document.getElementById('root').innerHTML = "[" + myarray + "]";
}
</script>
</body>
</html>
[false,false,false,false,false]
Using the for Loop
The for loop approach gives you complete control over array initialization. You can apply custom logic to determine each element's value based on its index or other conditions.
Syntax
// Initialize boolean array with for loop
let arrName = [];
for (let i = 0; i < length; i++) {
arrName.push(true/false);
}
Example
This example creates an alternating pattern of true and false values:
<html>
<body>
<h3>Initializing a boolean array using for loop</h3>
<p>Boolean array with true values in even positions and false values in odd positions</p>
<button onclick="bool()">Click Here</button>
<div id="root"></div>
<script>
function bool() {
// Initializing an empty array
let myarray = [];
for (let i = 0; i < 5; i++) {
if (i % 2 == 0) {
myarray.push(true);
} else {
myarray.push(false);
}
}
document.getElementById('root').innerHTML = "[" + myarray + "]";
}
</script>
</body>
</html>
[true,false,true,false,true]
Comparison
| Method | Use Case | Flexibility |
|---|---|---|
fill() |
Same value for all elements | Low |
Array.from() |
Custom logic with callback function | Medium |
for loop |
Complex conditions and patterns | High |
Conclusion
Use fill() for uniform values, Array.from() for simple patterns, and for loops when you need complex initialization logic. Each method serves different scenarios effectively.
