ProgramGuru

Nullish Coalescing

Learn Nullish Coalescing

Nullish coalescing (??) uses a replacement value only when the left operand is null or undefined.

In this lesson, you will study the rule, run a complete TypeScript example, explain its output, and test a small change.

What you’ll learn

  • Explain the main rule behind nullish coalescing.
  • Predict the result of the complete runnable TypeScript example.
  • Recognize one common mistake and correct it.

Understand the concept

Unlike ||, it preserves useful falsy values including 0, false and the empty string. ?? is particularly appropriate for optional settings, counts and explicit absence values. Parenthesize expressions when mixing it with && or || according to JavaScript grammar.

See the concept visually

Use the diagram to locate the checks, values or branches involved in the example.

Nullish Coalescing: illustrated flowLeft value: 0; Is it nullish? No; Keep 0; Otherwise use fallback1Left value: 02Is it nullish? No3Keep 04Otherwise usefallbackCompare the cases; they do not all necessarily execute.
Nullish Coalescing: illustrated flow. Left value: 0; Is it nullish? No; Keep 0; Otherwise use fallback.

Run a complete TypeScript example

Save this code as main.ts or paste it into the TypeScript Playground. The example uses fixed values so it runs without prompting for input.

TypeScript code

const savedCount: number | undefined = 0;
 const displayCount = savedCount ?? 10;
 const nickname: string | null = null;
 console.log(displayCount);
 console.log(nickname ?? "Guest");

Verified output

0
Guest

How to run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. When using a project configuration, run the matching project build instead.

Trace the execution

Because 0 is present rather than nullish, displayCount stays 0. Because nickname is null, the fallback Guest is printed.

Predict the output before you run the sample. Compare each line with the source expression that produced it, and distinguish the type checker’s work from what happens in JavaScript at runtime.

Common mistakes and how to avoid them

Do not replace ?? with || if zero or an empty string must remain meaningful.

Debugging habit: when a change is rejected, read the exact compiler diagnostic and inspect the operand types. When it compiles but behaves unexpectedly, follow the values through the JavaScript expressions.

Practice it yourself

Set savedCount to undefined and then 5. Compare savedCount ?? 10 with savedCount || 10 for the value 0.

Challenge: write down your prediction first, run the changed source, and explain any difference. Return to the verified version before continuing.

Check your understanding

Which operator preserves 0 while defaulting null?
Choose one answer. You can try again.

Answer and explanation

??

?? tests specifically for null and undefined, not general falsiness.

Review the key idea

Nullish Coalescing: Unlike ||, it preserves useful falsy values including 0, false and the empty string.

?? tests specifically for null and undefined, not general falsiness.

Before continuing, be able to explain what is checked before execution and what actually changes at runtime in the example above.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: if Statement →