What is the optional chaining operator?
Answer
The optional chaining operator (?., ES2020) allows accessing deeply nested properties without throwing an error if an intermediate value is null or undefined — it short-circuits and returns undefined. Before optional chaining: const city = user && user.address && user.address.city. With optional chaining: const city = user?.address?.city. It works for: property access (obj?.prop), bracket access (obj?.[key]), method calls (obj?.method()), and function calls (fn?.()). Combine with nullish coalescing for defaults: const city = user?.address?.city ?? "Unknown". Optional chaining is particularly useful when working with API responses where some fields may be absent, and when accessing deeply nested configuration or DOM properties. It avoids verbose null checks and makes code more readable.