Sub topic 4
Short-Circuiting with &&, ||, and ??
This chapter assumes you know which values are truthy and falsy, and that you can write if statements. The logical operators && and || do more than combine conditions. They return one of their operands, and they skip the right side when the left side already decides the result. The nullish coalescing operator ?? uses the same mechanism but tests a narrower condition.
The Operators Return Operands, Not Booleans
&& and || return the actual value of one of their operands, without converting it to a boolean:
a && breturnsaifais falsy. Otherwise it returnsb.a || breturnsaifais truthy. Otherwise it returnsb.
console.log("cat" && "dog"); // "dog"
console.log(0 && "dog"); // 0
console.log("" || "fallback"); // "fallback"
console.log("cat" || "fallback"); // "cat"
console.log(null || 0); // 0Inside an if condition the result is tested for truthiness, so you rarely notice the difference there. You do notice it when you assign the result to a variable.
Short-Circuit Evaluation
Short-circuiting means the right operand is never evaluated when the left operand already settles the answer. Any function calls or other side effects on the right side are skipped.
function announce(msg) {
console.log(msg);
return true;
}
false && announce("skipped by &&"); // prints nothing
true || announce("skipped by ||"); // prints nothing
true && announce("runs"); // prints "runs"
const user = null;
const name = user && user.name; // null, and no TypeError
console.log(name);The user && user.name pattern guards a property access. Modern code often uses optional chaining (user?.name) for this instead, which returns undefined rather than null.
Defaults and the 0-and-Empty-String Trap
|| is a common way to supply a default value, but it replaces every falsy value. That includes 0, "", and false, which are often valid data. The ?? operator returns the right side only when the left side is null or undefined.
const settings = { retries: 0, name: "" };
console.log(settings.retries || 3); // 3 (0 was thrown away)
console.log(settings.retries ?? 3); // 0
console.log(settings.name ?? "guest"); // ""
console.log(settings.timeout ?? 1000); // 1000| Operator | Returns the right side when the left side is | Typical use | Column | Column |
|---|---|---|---|---|
| `a && b` | truthy | Guarding an access or action | ||
| `a \ | \ | b` | falsy | Default when any falsy value means "missing" |
| `a ?? b` | `null` or `undefined` | Default that keeps `0`, `""`, and `false` |
Logical Assignment
Each operator has an assignment form that only assigns when the operator would pick the right side:
const config = { retries: 0, label: "" };
config.retries ??= 5; // stays 0
config.label ||= "none"; // becomes "none"
config.debug ??= false; // created as false
console.log(config); // { retries: 0, label: "none", debug: false }Like the plain operators, these short-circuit. When no assignment is needed, the right side is not evaluated and no write happens.
Common Errors
- Using
||for numeric or string defaults, which silently replaces a valid0or"". - Expecting
a && bto producetrueorfalse. UseBoolean(a && b)when you need an actual boolean. - Putting required side effects on the right of
&&or||and then being surprised when they don't run.
Exercise
A function reads const pageSize = options.pageSize || 20;. A caller passes { pageSize: 0 } to request "no limit", but gets 20. Fix the line, and explain what null || 0 evaluates to.
Show answer
Use const pageSize = options.pageSize ?? 20;. With ??, the default applies only when pageSize is null or undefined, so 0 is kept. null || 0 evaluates to 0: null is falsy, so || returns its right operand, and it returns it unchanged even though 0 is also falsy.
Exercise
What happens when you run const x = null || undefined ?? "default";, and how would you make it return "default"?
Show answer
It throws a SyntaxError, because ?? cannot be mixed with || without parentheses. Writing (null || undefined) ?? "default" works: the parenthesized part evaluates to undefined, so ?? returns "default".