Open In App

How to initialize a boolean array in JavaScript ?

The boolean array is a sequence of variables that can only carry the values true or false. A Boolean value can only be true or false and cannot have any other intermediate value. An array is a collection of data types that are stored at numerical places in a linear memory space. True and false values are defined by the JavaScript standard as a distinct data type known as a JavaScript Boolean. Booleans in JavaScript can be true, false, or (in some cases) a value that evaluates to true or false. It is critical to correctly maintain Boolean contexts since many JavaScript constructs rely on them.

Boolean arrays in JavaScript can be initialized using the methods:



Approach 1: Using Simple for loop

To initialize a Boolean array we can use a for loop to iterate over the array and add Boolean values to it.



Syntax:

// boolean array initialization using for loop
let arr_name = [];
for(initialize variable; condition; increment/decrement){
arr_name.push(false/true);
}

Example: In this example, we will use a simple for loop.




let arr1 = [];
for (let i = 0; i < 5; i += 1) {
    arr1.push(true);
}
console.log(arr1);

Output
[ true, true, true, true, true ]

Approach 2: Using fill() method

We can also use a fill() method. To use the fill() method we use the `Array()` constructor in JavaScript and create a new array of some given length and then use the inbuilt fill() method in JavaScript to initialize a Boolean array.

Syntax:

// boolean array initialization using fill() method
let arr_name = new Array(arr_length).fill(false/true);

Example: In this example, we will use fill() method.




let arr1 = new Array(5).fill(true);
console.log(arr1);

Output
[ true, true, true, true, true ]

Approach 3: Using from() method

The third method we can use to initialize a Boolean array is the from() method from JavaScript. To use this method we use the `Array` constructor and then call from the () method from it. In from() method we have to pass parameters length and (Val, index) which points towards the value(true/false).

Syntax:

// boolean array initialization using from() method
let arr_name = Array.from(
{length}, (val,index) => false/true);

Example: In this example, we will use from() method.




let arr1 = Array.from({ length: 5 }, (val, index) => false);
console.log(arr1);

Output
[ false, false, false, false, false ]

Approach 4: Using map() method

In this method use first initialize the `Array` constructor with the spread operator and then use the map() method to initialize the Boolean Array in JavaScript.

Syntax:

// boolean array initialization using map() method
const array_name = [...Array(length)].map(() => {
return false/true;
});

Example: In this example, we will use map() method.




const arr1 = [...Array(5)].map(() => {
    return false;
});
console.log(arr1);

Output
[ false, false, false, false, false ]

Approach 5: Initializing Boolean array Directly

We can also initialize the Boolean array directly by initializing an array in JavaScript and filling the value in it.

Syntax:

// Directly initializing Boolean Array in JavaScript
const arr_name = [values];

Example: In this example, we will initialize the Boolean array directly.




const arr1 = [true,true,true,true,true];
console.log(arr1);

Output
[ true, true, true, true, true ]

Article Tags :