HomeToolsAbout a20k

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 is true, it evaluates to A
    • when A is true and B is true, it evaluates to A
    • when A is true and B is false, it evaluates to A
  • When A is false, it evaluates to B
    • when A is false and B is true, it evaluates to B
    • when A is false and B is false, it evaluates to B

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 to B

    • when A is true and B is true, it evaluates to B
    • when A is true and B is false, it evaluates to B
  • When A is false, it evaluates to A

    • when A is false and B is true, it evaluates to A
    • when A is false and B is false, it evaluates to A

&& and || Interchangeability

// && to || bCondition1 && bCondition2 // is always equal to: !(!bCondition1 || !bCondition2) // || to && bCondition1 || bCondition2 // is always equal to: !(!bCondition1 && !bCondition2)
© VincentVanKoh