What is the nullish coalescing operator?

Answer

The nullish coalescing operator (??, ES2020) returns its right-hand side operand when the left-hand side is null or undefined — otherwise returns the left. const value = input ?? "default". This is different from the logical OR || operator which returns the right side for ANY falsy value (including 0, "", false). Example: const count = userCount ?? 0 — if userCount is 0, it stays 0 (0 is a valid value, not null/undefined). With ||, 0 || 0 would incorrectly return 0... wait, 0 || "default" would return "default" which is wrong if 0 is valid. The nullish assignment operator (??=): a ??= b assigns b to a only if a is null/undefined. Combine with optional chaining: user?.preferences?.theme ?? "dark".