Short Circuit Evaluations
How it Works
Though the short circuit evaluation coerces to Boolean for evaluation, the operators actually returns the value of one of the specified operands
If this operator is used with non-Boolean values, it will return a non-Boolean value
A || B
true || true; // true true || false; // true false || false; // false
- When
Aistrue, it evaluates toA- when
AistrueandBistrue, it evaluates toA - when
AistrueandBisfalse, it evaluates toA
- when
- When
Aisfalse, it evaluates toB- when
AisfalseandBistrue, it evaluates toB - when
AisfalseandBisfalse, it evaluates toB
- when
A && B
// returns first falsy element otherwise they return last element 1 && 0 && false // 0 1 && 2 && 3 // 3 (last)
-
When
Ais true, it evaluates toB- when
AistrueandBistrue, it evaluates toB - when
AistrueandBisfalse, it evaluates toB
- when
-
When
Aisfalse, it evaluates toA- when
AisfalseandBistrue, it evaluates toA - when
AisfalseandBisfalse, it evaluates toA
- when
&& and || Interchangeability
// && to || bCondition1 && bCondition2 // is always equal to: !(!bCondition1 || !bCondition2) // || to && bCondition1 || bCondition2 // is always equal to: !(!bCondition1 && !bCondition2)