JavaScript Reference ยท Operators
JavaScript logical operators
Reference for JavaScript logical operators &&, || and !: short-circuit evaluation, truthy and falsy values, returned operands and common patterns.
JavaScript has three logical operators: AND (&&), OR (||) and NOT (!). They work with truthy and falsy values, not just booleans. The falsy values are false, 0, -0, 0n, "", null, undefined and NaN; everything else is truthy.
The important twist: && and || do not return true or false. They return one of their operands, and they stop evaluating as soon as the result is known (short-circuiting).
a && b a || b !a
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
expr1 && expr2 // AND expr1 || expr2 // OR !expr // NOT
| Expression | Result |
|---|---|
a && b |
a if a is falsy, otherwise b |
a || b |
a if a is truthy, otherwise b |
!a |
true if a is falsy, otherwise false (always a boolean) |
Return value: ! always returns a boolean. && and || return the value of one of their operands.
Exceptions: none by themselves. Evaluating an operand can throw, but a skipped operand is never evaluated.
Usage notes
- Short-circuiting skips side effects. In
a && f(),fis only called whenais truthy. This makescond && doSomething()a (terse) alternative toif. ||for defaults is risky when0,""orfalseare valid values. Use??to replace onlynullandundefined.!!valueconverts any value to a boolean;Boolean(value)does the same and reads more clearly.- Precedence:
!binds tightest, then&&, then||.a || b && cisa || (b && c). Add parentheses when you mix them. - Mixing with
??: JavaScript refuses to parsea ?? b || cwithout parentheses. - Objects are always truthy, including
[],{}andnew Boolean(false). - Use in JSX-like rendering:
count && "items"renders0whencountis0. Compare explicitly (count > 0 && ...). - Logical assignment:
&&=and||=are covered on assignment operators.
Browser support
Supported in every browser and runtime.