Optional Chaining
Question
What does optional chaining (?.) do?
Hint
a?.b is shorthand for a == null ? undefined : a.b
Explanation
Optional chaining (?.) safely accesses nested properties. obj?.prop returns undefined if obj is null/undefined, instead of throwing. obj?.method?.() safely calls a method. arr?.[0] safely accesses array elements. This eliminates verbose null checks: instead of obj && obj.prop && obj.prop.value, write obj?.prop?.value. Optional chaining short-circuits — if any part is null/undefined, the entire expression evaluates to undefined. It is a JavaScript feature (ES2020), not TypeScript-specific.
Related Resources
Related Lessons
- Optional Chaining
Learn optional chaining.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.