Sub topic 10
Precedence Rules and Required Parentheses
This chapter assumes you know how ?? supplies defaults for null and undefined, how || and && short-circuit, and what operator precedence means: the rule that decides which operator grabs its operands first when an expression has no parentheses.
The ?? operator follows two rules that differ from most JavaScript operators. First, the language refuses to guess when ?? meets || or &&. Second, ?? binds loosely, so arithmetic and comparisons next to it are grouped before it runs. Both rules cause bugs or syntax errors if you write defaults inline without thinking.
The No-Mixing Rule
You cannot write ?? directly next to || or &&. The parser rejects the whole file with a SyntaxError before any code runs.
const a = null;
const b = 0;
// const bad1 = a || b ?? 5; // SyntaxError
// const bad2 = a ?? b && 5; // SyntaxError
const good1 = (a || b) ?? 5;
const good2 = a || (b ?? 5);
console.log(good1); // 0
console.log(good2); // 0The language designers made this choice on purpose. || treats 0 and "" as missing, and ?? does not. In an expression like a || b ?? 5, a reader can't tell which rule the author meant to apply at each step. Requiring parentheses means you have to state the grouping yourself.
The two groupings can produce different results:
const a = "";
const b = null;
console.log((a || b) ?? "default"); // "default" ("" || null gives null, then ?? replaces it)
console.log(a || (b ?? "default")); // "default"
const c = "";
const d = "set";
console.log((c ?? d) || "x"); // "set"
console.log(c ?? (d || "x")); // ""Where ?? Sits in the Precedence Table
?? sits near the bottom of the precedence table, at the same level as ||. Only the conditional operator (? :), assignment, arrow functions, yield and the comma bind more loosely.
| Operator group | Examples | Binds relative to `??` | Column | Column |
|---|---|---|---|---|
| Member access and calls | `.`, `?.`, `[]`, `()` | Much tighter | ||
| Arithmetic | `*`, `/`, `+`, `-` | Tighter | ||
| Comparison and equality | `<`, `>`, `===` | Tighter | ||
| Logical AND | `&&` | Cannot mix without parentheses | ||
| Logical OR, nullish | `\ | \ | `, `??` | Same level, cannot mix |
| Conditional | `? :` | Looser | ||
| Assignment | `=`, `??=` | Looser |
Optional chaining has member-access precedence, so user?.age ?? 18 already groups as (user?.age) ?? 18 and needs no parentheses.
The Arithmetic Trap
Because + binds tighter, an inline default inside a calculation often attaches to the wrong operand. This mistake doesn't raise an error. It just gives the wrong value.
const cart = { items: null };
const wrong = cart.items ?? 0 + 1; // parsed as cart.items ?? (0 + 1)
const right = (cart.items ?? 0) + 1;
console.log(wrong); // 1, but only by accident
const cart2 = { items: 4 };
console.log(cart2.items ?? 0 + 1); // 4 (the + 1 was never applied)
console.log((cart2.items ?? 0) + 1); // 5Comparisons fail the same way. count ?? 0 > 5 means count ?? (0 > 5), so a missing count becomes false instead of being compared.
The Ternary Case
The conditional operator binds more loosely than ??, so a ?? b ? x : y groups as (a ?? b) ? x : y. That is usually what you meant, but adding parentheses costs nothing and makes the grouping obvious to the next reader.
const theme = undefined;
const label = (theme ?? "light") === "dark" ? "Dark mode" : "Light mode";
console.log(label); // "Light mode"Exercise
What does const total = price ?? 10 * qty; compute when price is null and qty is 3, and when price is 5 and qty is 3? How would you rewrite it to mean "default price times quantity"?
Show answer
It parses as price ?? (10 * qty). With price null it gives 30; with price 5 it gives 5, and the quantity is ignored. Rewrite it as const total = (price ?? 10) * qty;, which gives 30 and 15.
Exercise
Does const x = flag && value ?? "none"; run? If not, fix it so that "none" replaces only a null or undefined result of flag && value.
Show answer
It throws a SyntaxError at parse time, because ?? cannot be mixed with && without parentheses. Write const x = (flag && value) ?? "none";.