0Pricing
TypeScript Academy · Lesson

Optional Chaining with ?.

Safely access deeply nested properties that may be null.

The Optional Chaining Operator

Optional chaining (?.) lets you safely access a property that might be on a null or undefined value. If the value is nullish, the whole expression short-circuits to undefined instead of throwing.

type User = { profile?: { bio?: string } };
const u: User = {};
const bio = u.profile?.bio;
console.log(bio); // undefined, no crash

Short-Circuiting on Nullish

When the operand before ?. is null or undefined, evaluation stops immediately and returns undefined. The rest of the chain is never executed.

const data: { value?: number } | null = null;
console.log(data?.value); // undefined, did not throw

All lessons in this course

  1. Optional Chaining with ?.
  2. Nullish Coalescing with ??
  3. Combining ?. and ??
  4. Optional Calls and Element Access
← Back to TypeScript Academy