Back to Lessons

Optional Chaining and Nullish

April 5, 2026

Optional Chaining (?.)

Safe property access without null/undefined errors.

Optional Chaining Examples

let user = {};
console.log(user?.profile?.address?.street); // undefined (no error)

// Arrays
let firstItem = arr?.[0];

// Function calls
user?.greet?.();

// Logical AND alternative
let value = user && user.name; // Old way
let value = user?.name ?? "Guest"; // New way

// Nullish coalescing (??)
let name = userName ?? "Anonymous"; // Only null/undefined

Key Points

  • ?. returns undefined if left is null/undefined.
  • No more (obj && obj.prop) chains.
  • ?? vs ||: only null/undefined vs falsy.
  • ES2020 feature, widely supported.