Reputation: 137
I've 3 variables a,b,c
The initial values of this variables will be 'false'
I want to compare this variables only have that 'true' value.
For example,
a=true,b=false,c=true.
So I need to compare like,
if(a && c)
if, a=true,b=true,c=true,
if(a && b && c)
How can I generate this if condition dynamically?
Upvotes: 1
Views: 1403
Reputation: 173562
I suppose you could approach the issue with [].every()
which performs a similar comparison:
if ([a, b, c].every(Boolean)) {
// etc.
}
Here, Boolean()
is a native function that will accept a value and return its boolean value.
That said, just this will cover all cases too, considering that it uses short circuiting to stop the comparison once a false
value is found and is undoubtedly easier to read:
if (a && b && c) {
// etc.
}
Upvotes: 5