Nullish Coalescing (??)

Med

Nullish coalescing returns right side only for null/undefined. Unlike || which treats 0 and "" as falsy.

Interactive Visualization

Destructuring

Old Way
const user = { name: "Alice", age: 25 };
const name = user.name;
const age = user.age;
Modern Way
Click transform to see

Key Points

  • a ?? b for null/undefined
  • Unlike || (0, "" matter)
  • Cannot mix with && ||
  • Use when 0/"" valid

Code Examples

?? vs ||

const value = 0;

// Wrong: || replaces 0
value || 100; // 100

// Right: ?? keeps 0
value ?? 100; // 0

const empty = "";
empty || "default"; // "default"
empty ?? "default"; // ""

?? only checks null/undefined, not all falsy values