Skip to content
Tutorials Center
My learning

Topics

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).

JavaScript
a && b
a || b
!a

Try it

JS Operands, not booleans

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Short-circuiting

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
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(), f is only called when a is truthy. This makes cond && doSomething() a (terse) alternative to if.
  • || for defaults is risky when 0, "" or false are valid values. Use ?? to replace only null and undefined.
  • !!value converts any value to a boolean; Boolean(value) does the same and reads more clearly.
  • Precedence: ! binds tightest, then &&, then ||. a || b && c is a || (b && c). Add parentheses when you mix them.
  • Mixing with ??: JavaScript refuses to parse a ?? b || c without parentheses.
  • Objects are always truthy, including [], {} and new Boolean(false).
  • Use in JSX-like rendering: count && "items" renders 0 when count is 0. Compare explicitly (count > 0 && ...).
  • Logical assignment: &&= and ||= are covered on assignment operators.

Browser support

Supported in every browser and runtime.