Interview Question

What is optional chaining?

Optional chaining safely short-circuits on null or undefined.

💡 Concept ✅ Quick Revision ⚡ JavaScript

Answer

Optional chaining stops a property access or call when its base value is null or undefined. • The `?.` operator returns undefined for the short-circuited chain. • It does not suppress errors from an undeclared root identifier. • Grouping can end the continuous optional chain.

Example

Code
const user = {};
console.log(user.address?.city);
Output
undefined

Quick Revision

Optional chaining safely short-circuits on null or undefined.