Bird
Raised Fist0
Typescriptprogramming~10 mins

Nullish coalescing with types in Typescript - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Nullish coalescing with types
Evaluate left side
↓
Is value null or undefined?
No→Use left side value
Yes↓
Use right side value
↓
Result assigned with correct type
The code checks if the left value is null or undefined; if yes, it uses the right value instead, preserving type safety.
Execution Sample
Typescript
let a: string | null = null;
let b: string = "default";
let c = a ?? b;
console.log(c);
Assigns 'c' to 'a' if 'a' is not null/undefined; otherwise assigns 'b'. Then prints 'c'.
Execution Table
StepExpression EvaluatedValueIs Nullish?Result Assigned to c
1anullYesb ("default")
2b"default"N/Ab ("default")
3c = a ?? b"default"N/A"default"
4console.log(c)prints "default"N/AN/A
💡 Execution ends after printing the value of c, which is "default" because a was null.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
anullnullnullnull
b"default""default""default""default"
cundefinedundefined"default""default"
Key Moments - 2 Insights
Why does 'c' get the value of 'b' instead of 'a'?
Because 'a' is null, which is considered nullish, so the nullish coalescing operator (??) chooses the right side 'b' as shown in execution_table step 1 and 3.
What if 'a' was an empty string ""? Would 'c' still get 'b'?
No, because an empty string is not null or undefined, so 'c' would get the empty string value of 'a'. The operator only checks for null or undefined, not falsy values.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 1, what is the value of 'a' and is it nullish?
A"default", No
Bnull, Yes
Cundefined, Yes
D"", No
💡 Hint
Check the 'Value' and 'Is Nullish?' columns in execution_table row 1.
At which step is the variable 'c' assigned its final value?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look for the assignment expression 'c = a ?? b' in execution_table.
If 'a' was "hello" instead of null, what would 'c' be after step 3?
A"default"
Bnull
C"hello"
Dundefined
💡 Hint
Recall that ?? uses left value if it is not null or undefined, see key_moments explanation.
Concept Snapshot
Nullish coalescing (??) returns the left value if it is NOT null or undefined.
Otherwise, it returns the right value.
Useful for default values without overriding falsy values like 0 or "".
TypeScript infers the precise result type by excluding null/undefined from the left side's type and unioning with the right side's type.
Syntax: let result = value1 ?? value2;
Full Transcript
This example shows how the nullish coalescing operator (??) works in TypeScript with types. We start with variable 'a' which is null, and 'b' which is a string "default". When we assign 'c = a ?? b', the operator checks if 'a' is null or undefined. Since 'a' is null, it uses 'b' instead. The final value of 'c' is "default". TypeScript infers the type of 'c' as string, preserving type safety since the result cannot be null or undefined. The console.log prints "default". If 'a' was an empty string, 'c' would be that empty string, not 'b', because empty string is not nullish. This operator helps provide default values only when the left side is truly missing (null or undefined), unlike || which treats all falsy values the same.

Practice

(1/5)
1.

What does the nullish coalescing operator ?? do in TypeScript?

easy
A. Throws an error if the left value is null or undefined.
B. Returns the right value if the left value is any falsy value like 0 or empty string.
C. Always returns the left value regardless of its content.
D. Returns the right value only if the left value is null or undefined.

Solution

  1. Step 1: Understand nullish coalescing behavior

    The operator ?? checks if the left value is null or undefined.
  2. Step 2: Determine when right value is returned

    If the left value is null or undefined, it returns the right value; otherwise, it returns the left value.
  3. Final Answer:

    Returns the right value only if the left value is null or undefined. -> Option D
  4. Quick Check:

    Null or undefined triggers right value [OK]
Hint: Only null or undefined triggers right side [OK]
Common Mistakes:
  • Confusing with || operator that checks all falsy values
  • Thinking 0 or empty string triggers right value
  • Assuming it throws errors on null or undefined
2.

Which of the following is the correct syntax to use nullish coalescing in TypeScript?

const value = input ?? 'default';
easy
A. const value = input ?? 'default';
B. const value = input ??= 'default';
C. const value = input || 'default';
D. const value = input ? 'default' : input;

Solution

  1. Step 1: Identify correct nullish coalescing syntax

    The operator ?? is used between two values to provide a fallback if the left is null or undefined.
  2. Step 2: Check each option

    const value = input ?? 'default';: const value = input ?? 'default'; uses ?? correctly. const value = input ? 'default' : input; uses ternary, const value = input ??= 'default'; uses nullish assignment ??=, const value = input || 'default'; uses logical OR.
  3. Final Answer:

    const value = input ?? 'default'; -> Option A
  4. Quick Check:

    Correct nullish coalescing syntax uses ?? [OK]
Hint: Look for ?? between values, not ?: or || [OK]
Common Mistakes:
  • Using || which treats 0 or empty string as false
  • Confusing with ternary operator syntax
  • Using invalid operators like ??=
3.

What is the output of this TypeScript code?

let name: string | null = "";
const displayName = name ?? "Guest";
console.log(displayName);
medium
A. "Guest"
B. null
C. "" (empty string)
D. undefined

Solution

  1. Step 1: Check the value of name

    The variable name is an empty string "", which is NOT null or undefined.
  2. Step 2: Apply nullish coalescing operator

    Since name is not null or undefined, name ?? "Guest" returns the left value, which is "" (empty string).
  3. Final Answer:

    "" (empty string) -> Option C
  4. Quick Check:

    Empty string is not nullish, so left value stays [OK]
Hint: Empty string is NOT nullish, so ?? returns left [OK]
Common Mistakes:
  • Assuming empty string triggers right side
  • Confusing ?? with || operator
  • Expecting 'Guest' instead of empty string
4.

Identify the error in this TypeScript code using nullish coalescing:

let count: number | undefined = 0;
const result = count ?? 10;
console.log(result);
medium
A. Error: nullish coalescing cannot be used with numbers
B. No error; output is 0
C. Error: count cannot be 0
D. Error: must use || instead of ??

Solution

  1. Step 1: Analyze the value of count

    The variable count is 0, which is NOT null or undefined.
  2. Step 2: Apply nullish coalescing operator

    Since count is 0 (a valid number), count ?? 10 returns 0, no error occurs.
  3. Final Answer:

    No error; output is 0 -> Option B
  4. Quick Check:

    0 is not nullish, so left value returned [OK]
Hint: 0 is valid, ?? returns left, no error [OK]
Common Mistakes:
  • Thinking 0 triggers right side fallback
  • Believing ?? cannot be used with numbers
  • Confusing ?? with || operator
5.

You have a function that accepts an optional string parameter input. You want to return input if it is not null or undefined, but if it is, return the string "No input". Which is the best way to write this using nullish coalescing?

function getInput(input?: string): string {
  // Fill in the return statement
}
hard
A. return input ?? "No input";
B. return input || "No input";
C. return input ? input : "No input";
D. return input === null || input === undefined ? "No input" : input;

Solution

  1. Step 1: Understand the requirement

    Return input if it is NOT null or undefined; otherwise, return "No input".
  2. Step 2: Choose the best nullish coalescing usage

    input ?? "No input" returns input if it is not nullish, else "No input". This is concise and clear.
  3. Final Answer:

    return input ?? "No input"; -> Option A
  4. Quick Check:

    ?? returns left unless null/undefined [OK]
Hint: Use ?? to check only null or undefined [OK]
Common Mistakes:
  • Using || which treats empty string as false
  • Using ternary unnecessarily
  • Checking null and undefined manually instead of ??
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard