What is optional chaining and how does it help?
Quick answer
`?.` safely reads deep properties, short-circuiting to undefined instead of throwing when a link is null/undefined.
In detail
Optional chaining avoids 'cannot read property of undefined' errors when accessing nested data. It works for properties (`a?.b`), calls (`fn?.()`), and dynamic keys (`a?.[k]`). Pair it with nullish coalescing for a clean default.
const res = { data: { user: null } };
res.data?.user?.name; // undefined (no crash)
res.fetch?.(); // safe optional call
res.data?.user?.name ?? "Guest"; // "Guest"Why interviewers ask this: Tests familiarity with modern, safer access patterns.
This is 1 of 118+ questions in the JavaScript Interview Kit
Get every question with detailed answers, follow-ups and real code — plus coding challenges and a last-minute revision sheet. One-time payment, instant access.
⚡ Get the JavaScript Interview Kit → ₹299