Nullish Coalescing in TypeScript
Provide default values with the ?? operator, which only triggers on null and undefined.
What you'll learn
- How the ?? operator differs from the || operator
- Why ?? only treats null and undefined as empty
- Combining ?? with optional chaining for safe defaults
- Common patterns: config values, API responses, and form inputs
Concept
?? vs ||
The nullish coalescing operator (??) returns the right side when the left is null or undefined. This differs from the logical OR (||) which returns the right side for any falsy value (including 0, "", and false). This distinction matters: count || 10 returns 10 when count is 0, but count ?? 10 returns 0 because 0 is not nullish. If 0 is a valid value, you must use ??.
When to Use ??
Use ?? whenever the left value could legitimately be a falsy non-null value. Common cases: numeric counters (0 is valid), empty strings ("" is valid), boolean flags (false is valid), and configuration options where the user explicitly set a falsy value. The || operator is still appropriate when you genuinely want to replace all falsy values, like filtering empty strings from a list.
Combining with Optional Chaining
The ?? operator pairs perfectly with optional chaining (?.). Since ?. returns undefined on short-circuit, ?? provides the fallback: config?.timeout ?? 5000 returns the configured timeout if it exists, or 5000 as a default. This one-liner replaces a multi-line conditional check and is the idiomatic way to handle "deeply nested optional value with a default" in modern TypeScript.
Example
// ?? only triggers on null and undefined — 0 and "" are preserved.
let count: number | null = 0;
console.log(count ?? 10); // 0 (not 10, because 0 is not nullish)
console.log(count || 10); // 10 (|| treats 0 as falsy)
let label: string | undefined = "";
console.log(label ?? "default"); // "" (empty string is preserved)
console.log(label || "default"); // "default" (|| treats "" as falsy)
// Combining ?. with ?? for nested defaults.
interface Config {
server?: {
port?: number;
host?: string;
};
}
function getPort(config: Config): number {
return config?.server?.port ?? 3000;
}
function getHost(config: Config): string {
return config?.server?.host ?? "localhost";
}
console.log(getPort({})); // 3000
console.log(getPort({ server: { port: 8080 } })); // 8080
console.log(getHost({ server: { host: "" } })); // "" (valid empty host)
The ?? operator returns the right side only for null/undefined, preserving 0, empty string, and false. Use it instead of || when falsy non-null values are valid. Pair with ?. for nested defaults.
Try it
- JSON Validator
Validate JSON with nullable fields before applying ?? defaults.
- JSON to TypeScript
Generated types show where ?? is needed for nullable fields.
Common mistakes
The mistake
Using || when 0, false, or empty string are valid values
The fix
|| replaces all falsy values. If 0, false, or "" are legitimate, use ?? which only replaces null and undefined. This is a common source of subtle bugs.
The mistake
Chaining multiple ?? operators expecting the first non-null value
The fix
?? is left-associative and only checks one level. For choosing among multiple candidates, use a helper or nested ternary. Each ?? checks only its immediate left operand.
Practice Nullish Coalescing in TypeScript
4 exercises
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.