short-circuit evaluation
Short-circuit evaluation means JavaScript stops evaluating an expression as soon as its left operand settles the result, so the operands to the right never run. It applies to the logical AND (&&), logical OR (||), and nullish coalescing (??) operators, to optional chaining (?.), and to the logical assignment operators (&&=, ||=, ??=).
A skipped operand isn’t evaluated at all. A function call on that side isn’t made, an increment doesn’t happen, a setter isn’t called, and an error it would throw is never raised. The result is one of the operands, unchanged, so &&, ||, and ?? don’t convert it to a boolean.
How JavaScript Short-Circuit Evaluation Works
Each operator tests its left operand and evaluates the right side only when the test can’t decide the result:
| Operator | Stops when the left operand is | Result when it stops |
|---|---|---|
&& |
Falsy | The left operand |
|| |
Truthy | The left operand |
?? |
Neither null nor undefined |
The left operand |
?. |
null or undefined |
undefined |
&&=, ||=, ??= |
The same as &&, ||, or ?? |
The current value, with no assignment |
Optional chaining skips more than one operand. When the value before ?. is null or undefined, the rest of the chain is skipped too, including later property reads, calls, and the expressions inside brackets. A pair of parentheses ends the chain.
Logical assignment builds on the same rule. The expression x ||= y means x || (x = y), so the assignment itself is the right operand and never runs when x is truthy.
JavaScript Short-Circuit Evaluation Examples
Say a settings screen should load the default settings only when no saved settings exist. Counting the calls to loadDefaults() shows when the right operand runs:
> let calls = 0;
undefined
> function loadDefaults() {
… calls += 1;
… return { theme: "light" };
… }
undefined
> const saved = { theme: "dark" };
undefined
> saved ?? loadDefaults()
{ theme: 'dark' }
> calls
0
> undefined ?? loadDefaults()
{ theme: 'light' }
> calls
1
With saved settings, ?? returns saved and never calls loadDefaults(). With undefined on the left, it evaluates the right operand, and the counter goes up. The && and || operators skip their right side the same way, based on truthiness instead.
An order may arrive without a customer. Optional chaining stops at the missing customer, but grouping part of the chain changes the outcome:
> const order = { id: 7, customer: null };
undefined
> order.customer?.address.city
undefined
> (order.customer?.address).city
Uncaught TypeError: Cannot read properties of undefined (reading 'city')
In the first expression, ?. skips both .address and .city. In the second, the parentheses end the chain at .address, so .city is read from undefined and throws.
JavaScript Short-Circuit Evaluation Gotchas
- Code with a side effect on the skipped side, such as a log call or a counter update, doesn’t run, and nothing reports that it was skipped.
- An error on the skipped side stays hidden, so a typo in
isReady && sendReport()surfaces only onceisReadyis truthy. - A logical assignment that doesn’t assign also doesn’t throw, so
count ||= 5succeeds on aconstbinding whose value is truthy.
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Nov. 12, 2026