← All tutorials

GoCareerGo Tutorials

JavaScript Tutorial

Variables to closures, promises, the DOM and OOP — a complete JS reference.

Optional Chaining (?.)

Safely reads a deeply nested property without throwing if an intermediate value is null or undefined.

const user = { profile: null };
user.profile?.address?.city; // undefined, no error
// Without ?. this would throw: Cannot read properties of null
  • Short-circuits to undefined the moment it hits null/undefined
  • Works for property access (?.), function calls (?.()) and array indexing (?.[0])
  • Pairs perfectly with the nullish coalescing operator (??) for a default value