How to usefill() method in Javascript
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.
Javascript
let arr1 = new Array(5).fill( 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