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
A
istrue
, it evaluates toA
- when
A
istrue
andB
istrue
, it evaluates toA
- when
A
istrue
andB
isfalse
, it evaluates toA
- when
- When
A
isfalse
, it evaluates toB
- when
A
isfalse
andB
istrue
, it evaluates toB
- when
A
isfalse
andB
isfalse
, 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
A
is true, it evaluates toB
- when
A
istrue
andB
istrue
, it evaluates toB
- when
A
istrue
andB
isfalse
, it evaluates toB
- when
-
When
A
isfalse
, it evaluates toA
- when
A
isfalse
andB
istrue
, it evaluates toA
- when
A
isfalse
andB
isfalse
, it evaluates toA
- when
&&
and ||
Interchangeability
// && to || bCondition1 && bCondition2 // is always equal to: !(!bCondition1 || !bCondition2) // || to && bCondition1 || bCondition2 // is always equal to: !(!bCondition1 && !bCondition2)