JS: Boolean Operators
Logical-and operator
x && y-
- Return
trueif bothBoolean(x)andBoolean(y)aretrue, elsefalse. - If
Boolean(x)isfalse, y is not evaluated.
// “and” operator console.log(true && true); - Return
Logical-or operator
x || y-
- Return
trueif one ofBoolean(x)orBoolean(y)istrue, else returnfalse. - If
Boolean(x)istrue, y is not evaluated.
// “or” operator console.log(true || true); - Return
Logical-not operator
!x-
- Reverse the boolean value.
- Return
trueifBoolean(x)isfalse, returnfalseifBoolean(x)istrue.
// “logical negation” operator console.log(!false);
Operator precendence
precedence from highest to lowest is:
!(logical NOT). highest precedence.&&(logical AND)||(logical OR)
!a && b || c // parsed as: ((!a) && b) || c
and
!is right-associative.&&is left-associative.||is left-associative.
a && b && c; // means // (a && b) && c a || b || c; // means // (a || b) || c !!a; // means // !(!a)