Lesson 27 of 80
Two operators that deliberately check for exactly null/undefined and nothing else — the fix for coercion's biggest everyday annoyance: falsy values getting treated like they're missing.
10 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 2
`const label = count || "default";` where `count` is `0`. What does `label` end up being — and is that a bug?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
const city = user?.address?.city;
// reads city only if user AND user.address are both non-nullish;
// otherwise short-circuits to undefined instead of throwing
const label = count ?? "no items";
// "no items" only if count is null or undefined — NOT if count is 0
Optional chaining (?.) stops a property-access chain the moment it hits null/undefined, returning undefined instead of throwing. Nullish coalescing (??) picks the right side only when the left is null or undefined — deliberately narrower than ||, which triggers on any falsy value.
const count = 0;
count || "no items"; // "no items" — 0 is falsy, so || wrongly treats it as missing
count ?? "no items"; // 0 — correct, 0 is a real value, not a missing one
user?.address?.city — and user is null.
?. short-circuits the entire rest of the chain, not just the one property — a?.b.c.d returns undefined immediately if a is nullish, without ever attempting to read .c or .d.?.() and ?.[] exist too — optional call (obj.method?.(), safe even if method doesn't exist) and optional computed access (obj?.[key]), not just plain dot access.?? directly with || or && without parentheses — a || b ?? c is a SyntaxError. The language forces you to be explicit about precedence here rather than guess.Recognizing x || default as a latent bug when x can be 0 or "" is a sharp, practical signal — it shows you understand truthiness deeply enough to know exactly when || is the wrong tool, not just that ?? exists.