The nullish coalescing operator (??) provides a fallback value only when the left side is null or undefined, unlike the logical OR (||) which treats all falsy values (0, '', false) as requiring the default.
// ❌ Wrong - empty string is falsy
const title = post.title || "Untitled";
// ✅ Better - only null/undefined trigger default
const title = post.title ?? "Untitled";
// Examples
const score = 0;
console.log(score || 100); // 100 (wrong!)
console.log(score ?? 100); // 0 (correct!)
const empty = "";
console.log(empty ?? "default"); // '' (preserves empty string)