MediumModern JS💻 Output Question

Optional chaining prevents TypeError on missing properties

💡

Hint

?. returns undefined instead of throwing when a link in the chain is null/undefined.

What does this output?

const user = { profile: { city: 'NY' } };
console.log(user?.profile?.city);
console.log(user?.settings?.theme);

Correct Output

NY
undefined

Why this output?

Explanation: user?.profile?.city: chain exists, returns "NY". user?.settings returns undefined, ?.theme short-circuits to undefined — no TypeError.

Key Insight: ?. returns undefined instead of throwing when a link in the chain is null/undefined.

More Modern JS Output Questions

EasyDestructuring defaults apply only for undefinedEasyObject spread — later properties winMediumNullish coalescing ?? vs OR ||EasyTemplate literal expressions

Practice predicting output live →

66 output questions with instant feedback

💻 Try Output Quiz