Sub topic 12
Precedence Rules and Why ?? Needs Parentheses
This chapter assumes you already know how &&, || and ?? choose which operand to return, and how short-circuit evaluation skips the right-hand side. Here the question is grouping. When one expression contains several logical operators, which one runs first?
The Precedence Order
Precedence decides how JavaScript groups operands before it evaluates anything. Operators with higher precedence bind more tightly.
| Operator | Meaning | Relative precedence | Column | Column |
|---|---|---|---|---|
| `!` | Logical NOT | Highest of the group | ||
| `&&` | Logical AND | Higher than ` | ` | |
| ` | ` | Logical OR | Lower than `&&` | |
| `??` | Nullish coalescing | Same level as ` | `, but cannot be mixed with it | |
| `? :` | Conditional (ternary) | Lower than all of the above |
So ! applies first, then &&, then ||. Precedence only controls grouping. Each operator still evaluates its own operands left to right and short-circuits as usual.
const a = false;
const b = true;
const c = "c";
// && binds tighter than ||, so this is a || (b && c)
console.log(a || b && c); // "c"
// ! binds tighter than &&, so this is (!a) && c
console.log(!a && c); // "c"
// Parentheses change the grouping
console.log((a || b) && c); // "c"
console.log(a || (b && 0)); // 0Why ?? Refuses to Mix
Mixing || and && is legal because their relative precedence is defined. For ??, the language designers chose to forbid mixing with either operator unless you add parentheses. Developers often misread a || b ?? c and assume one grouping when the parser would pick another. A SyntaxError forces you to state which grouping you mean.
The error happens at parse time. The script never starts, so you cannot catch it with try in the same file. To see it from running code, you can parse the source text separately:
try {
new Function('return null || undefined ?? "default"');
} catch (err) {
console.log(err.name); // "SyntaxError"
}
// Explicit grouping is accepted
const x = (null || undefined) ?? "default";
console.log(x); // "default"
const y = null || (undefined ?? "default");
console.log(y); // "default"In this example both groupings give the same result. With other values they can differ:
const count = 0;
const fallback = 10;
const enabled = true;
console.log((count ?? fallback) && enabled); // 0
console.log(count ?? (fallback && enabled)); // 0
const missing = null;
console.log((missing ?? fallback) && enabled); // true
console.log(missing ?? (fallback && enabled)); // true
console.log((enabled && missing) ?? fallback); // 10
console.log(enabled && (missing ?? fallback)); // 10The results match here too, but the meaning differs. The first form of each pair applies the default before the && test, and the second applies it inside the test. Write the parentheses that match what you mean.
What Is Still Allowed
The restriction only covers putting ?? directly next to && or ||. These forms are fine:
const user = { nickname: null, name: "Ada" };
// Chaining ?? with itself
console.log(user.nickname ?? user.alias ?? user.name); // "Ada"
// ! binds tighter, so this is (!user.nickname) ?? false
console.log(!user.nickname ?? false); // true
// The ternary has lower precedence, so this is (user.nickname ?? "guest") ? ... : ...
console.log(user.nickname ?? "guest" ? "has label" : "no label"); // "has label"Common Errors
- Writing
a || b ?? cand getting a SyntaxError. Add parentheses around the part you want evaluated first. - Assuming
a || b && cmeans(a || b) && c. It actually meansa || (b && c). - Expecting
!to apply to a whole expression.!a && bnegates onlya. Write!(a && b)to negate the result.
Exercise
What does console.log(0 || null && "x") print, and does console.log(0 || null ?? "x") run?
Show answer
The first prints 0. && binds tighter, so it is 0 || (null && "x"). The inner part returns null, and 0 || null returns null... correction: 0 || null returns its last operand, null, so the first line prints null. The second line is a SyntaxError because ?? is mixed with || without parentheses, so the script never runs.
Exercise
Rewrite const port = config.port || env.port ?? 8080; so it parses and uses env.port only when config.port is null or undefined, and 8080 only when env.port is also null or undefined.
Show answer
Use ?? throughout: const port = config.port ?? env.port ?? 8080;. Chaining ?? with itself needs no parentheses.