JavaScript can compare an array containing Boolean values using various logic operators such as AND (&&) or OR (||). For instance, to check if all the elements in the array are true, we can use the AND operator as follows:
let booleanArray = [true, true, false];
let allTrue = booleanArray.every(element => element); // returns false
The every
method checks if every element in the array satisfies the provided condition and returns a Boolean value. In the above code, allTrue
variable will be false because the third element in the array is false. Similarly, to check if any element in the array is true, we can use the OR operator as follows:
let anyTrue = booleanArray.some(element => element); // returns true
The some
method checks if any element in the array satisfies the provided condition and returns a Boolean value. In the above code, anyTrue
variable will be true because at least one element in the array is true.
Asked: 2023-01-28 11:00:00 +0000
Seen: 10 times
Last updated: May 23 '22