Skip to the solution
EasyFrontend5 min read

What are optional chaining and nullish coalescing?

872 views

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

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

5 min read Β· 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

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

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 12 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextEasy

What is the difference between includes and indexOf?

Continue