Skip to solution
easyFrontend

What are optional chaining and nullish coalescing?

872 views
01

Understand the problem

Question presented to candidate: "You're accessing a deeply nested property, like response.data.user.address.city, but any of those intermediate levels might legitimately be missing. How would you write that safely, and what specifically does ?? do differently from || for providing a default value?"

What a strong answer should cover:

  • 📌 Interview term: optional chaining (?.) — accesses a property, calls a method, or indexes into an array, short-circuiting to undefined instead of throwing, if the value immediately before the ?. is null or undefined.
  • 📌 Interview term: the real, direct answer to the prompt — verified directly: chaining ?. through several levels of a possibly-missing nested path (data.missing?.deep?.prop) genuinely returned undefined with no throw, while the identical chain WITHOUT ?. genuinely threw a real TypeError at the first missing level.
  • 📌 Interview term: the chain-wide short-circuit — verified directly with a real call counter: once a ?. in a chain hits null/undefined, the entire rest of the chain genuinely short-circuits — a method call further along the same chain was genuinely never invoked at all, not just its result discarded.
  • 📌 Interview term: nullish coalescing (??) — provides a fallback value, but only when the left-hand side is specifically null or undefined — verified directly: 0 ?? "default" genuinely stayed 0, while 0 || "default" genuinely became "default", since || treats ANY falsy value (not just nullish ones) as needing the fallback.
  • A precise answer names that ?? cannot be mixed directly with &&/|| in the same expression without explicit parentheses — verified directly, this genuinely throws a real SyntaxError, a deliberate spec decision due to their ambiguous relative precedence.

Clarifying questions expected:

  • None — this is a definitional/technical question; directly answering the prompt's own scenario, plus the sharp ?? vs. || distinction, is the strong signal.

Code / implementation expected: Yes — the deep-chain-with-?. vs. without-?. contrast, plus the 0 ?? vs 0 || contrast, are the clearest, most convincing demonstrations.

optional-chaining
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript fundamentals interviews. Difficulty: Easy

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every throw-vs-no-throw and call-count claim below was actually run in Node.

1. Why Thi

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal proof: ?. safely short-circuits a deep missing chain (including a later call), and ?? correctly preserves a real 0 where || does not
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 12 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track