Sub topic 16
Logical Operators: ||, &&, and ??
This chapter assumes you know the truthy and falsy rules from the ToBoolean chapter, and that you can tell null from undefined. The logical operators use those rules, but they don't return booleans. Each one returns one of its operands.
What Each Operator Returns
a || bchecks whetherais truthy. If it is, the result isa. Otherwise the result isb.a && bchecks whetherais falsy. If it is, the result isa. Otherwise the result isb.a ?? bchecks whetheraisnullorundefined. If it is, the result isb. Otherwise the result isa.
All three short-circuit: they only evaluate b when its value is needed.
console.log("a" || "b"); // "a": first truthy operand
console.log("" || "b"); // "b"
console.log("" && "next"); // "": first falsy operand
console.log("x" && "next"); // "next"
console.log(0 ?? 5); // 0: 0 is not null or undefined
console.log(null ?? 5); // 5
let calls = 0;
const hit = () => { calls++; return "fallback"; };
"set" || hit();
console.log(calls); // 0: hit() never ranThe Common Bug: || for Defaults
|| falls back on every falsy value, including 0, "", NaN, and false. Those are often valid inputs.
function setVolume(level) {
const withOr = level || 50;
const withNullish = level ?? 50;
return [withOr, withNullish];
}
console.log(setVolume(0)); // [50, 0]: || discarded a valid 0
console.log(setVolume(undefined)); // [50, 50]Use ?? when only a missing value should trigger the default. Use || when you deliberately want any falsy value, such as an empty string, replaced.
The assignment forms behave the same way. x ||= y assigns when x is falsy. x &&= y assigns when x is truthy. x ??= y assigns only when x is null or undefined.
Optional Chaining
?. stops and returns undefined when the value on its left is null or undefined, so you get no TypeError. It pairs naturally with ??:
const user = null;
console.log(user?.name); // undefined
console.log(user?.name ?? "Guest"); // "Guest"
const settings = { theme: "" };
console.log(settings?.theme ?? "light"); // "": empty string is kept
console.log(settings.save?.()); // undefined: method call skippedCommon Errors
| Mistake | Effect | Fix | Column | Column |
|---|---|---|---|---|
| `count \ | \ | 10` with a valid `0` | Returns 10 | `count ?? 10` |
| Expecting `a && b` to be boolean | Returns an operand | `Boolean(a && b)` | ||
| `a \ | \ | b ?? c` | SyntaxError | Add parentheses |
| `obj.a.b` on a missing `a` | TypeError | `obj.a?.b` |
Exercise
What do these log? console.log(NaN || "x"); console.log(NaN ?? "x"); console.log(null && f()); console.log("" ?? "empty");
Show answer
"x" (NaN is falsy), NaN (NaN is not null or undefined), null (&& returns its first falsy operand, so f is never called), and "" (the empty string is kept because it is not nullish).
Exercise
A config has retries: 0. Why does const r = config.retries || 3; cause a bug, and what should replace it?
Show answer
0 is falsy, so || returns 3 and ignores the configured value. Use config.retries ?? 3, which falls back only when retries is null or undefined.