← Back to Roadmap

Optional Chaining (?.)

Easy

In plain terms

Optional chaining ?. safely accesses nested properties. If any part is null/undefined, the expression short-circuits to undefined instead of throwing. Use for uncertain object chains.

What you need to know

  • ?. short-circuits on null/undefined
  • Safe property/method/index access
  • ES2020

Try it yourself

Copy the code below and run it in your browser console or a code editor:

const user = { address: { city: 'NYC' } };
user?.address?.city;  // "NYC"

const u = null;
u?.address?.city;    // undefined (no error)

// Optional method call
obj?.method?.();

// Optional indexing
arr?.[0];

// With nullish coalescing
const name = user?.profile?.name ?? 'Anonymous';

Learn more

Dive deeper with these trusted resources: