TypeScript Output Prediction Medium +20 XP
Optional Chaining
What is the output?
TypeScriptRead-only
const user = { name: "Alice", address: undefined };
console.log(user.address?.city ?? "Unknown");What is the output?
Show answer and explanation
Answer: Unknown
address is undefined, so ?.city returns undefined. The ?? operator returns 'Unknown' as the fallback.
Hint: ?. short-circuits on undefined.
What this challenge tests
Output Prediction in TypeScript: optional-chaining, nullish-coalescing.