Sub topic 7
Short-Circuit Evaluation: When the Right Side Never Runs
This chapter assumes you know how || returns the first truthy operand and && returns the first falsy operand, as covered in the previous two chapters. You should also be comfortable writing functions and reading console.log output.
The rule
Short-circuit evaluation means JavaScript stops evaluating a logical expression as soon as the result is known. The remaining operands are never evaluated. Any function calls, property reads, or assignments inside them do not happen.
- With
a && b, ifais falsy, the result isa, andbis skipped. - With
a || b, ifais truthy, the result isa, andbis skipped.
This rule goes beyond saving work. Because the right side may not run, you can use && and || to control whether side effects happen.
Proving the right side is skipped
A function that logs when it runs makes the behavior visible:
function log(msg) {
console.log(msg);
return msg;
}
false && log("skipped"); // nothing is logged
true || log("skipped"); // nothing is logged
true && log("runs"); // logs "runs"In the first two lines log is never called. The left operand already decided the result, so JavaScript never looks at the right operand.
Chains stop at the deciding operand
Longer chains are evaluated left to right, and evaluation stops at the first operand that decides the result:
function step(name, value) {
console.log("evaluated", name);
return value;
}
const r1 = step("a", 1) && step("b", 0) && step("c", 2);
// evaluated a
// evaluated b
console.log(r1); // 0
const r2 = step("x", "") || step("y", "ok") || step("z", "never");
// evaluated x
// evaluated y
console.log(r2); // "ok"step("c", 2) and step("z", "never") never run.
Using short-circuiting deliberately
The most common use is a guard. A guard checks a condition on the left before an operation on the right that would fail or be wasteful without it.
const user = null;
// Without the guard, user.name would throw a TypeError
const name = user && user.name;
console.log(name); // null
const debug = false;
debug && console.log("Debug info"); // nothing is logged
const cache = {};
function expensiveLookup(key) {
console.log("computing", key);
return key.length;
}
const len = cache.word || expensiveLookup("word");
// computing word
console.log(len); // 4Common errors
| Mistake | What happens | Column | Column |
|---|---|---|---|
| Expecting a function on the right to always run | It runs only when the left side does not decide the result | ||
| Putting a required assignment on the right | `ok | (count += 1)` never increments when `ok` is truthy | |
| Assuming `&` or `\ | ` short-circuit | Bitwise operators always evaluate both sides |
The last row is easy to check:
function hit() {
console.log("hit");
return 1;
}
0 & hit(); // logs "hit", because & evaluates both operands
0 && hit(); // nothing is loggedExercise
What does this code log, and what is the final value of count?
let count = 0;
const inc = () => ++count;
const result = (count > 0 && inc()) || inc() || inc();
console.log(result, count);
Show answer
It logs 1 1. count > 0 is false, so inc() inside the parentheses is skipped and the parentheses evaluate to false. || then evaluates the next inc(), which returns 1 (truthy). That decides the result, so the last inc() never runs and count stays at 1.
Exercise
Why does user && user.name avoid a TypeError when user is null?
Show answer
null is falsy, so && returns null without evaluating user.name. The property access that would throw never runs.