How to useSimple for loop in Javascript
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.
Javascript
let arr1 = []; for (let i = 0; i < 5; i += 1) { arr1.push( true ); } console.log(arr1); |
[ true, true, true, true, true ]
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:
Table of Content
- Using Simple for loop
- Using fill() method
- Using from() method
- Using map() method
- Initializing Boolean array Directly