What does the nullish coalescing operator (??) do in TypeScript?
It returns the right-hand side value only if the left-hand side value is null or undefined; otherwise, it returns the left-hand side value.
Click to reveal answer
intermediate
How does nullish coalescing differ from the logical OR (||) operator?
Nullish coalescing (??) only treats null and undefined as empty, while || treats any falsy value (like 0, '', false) as empty.
Click to reveal answer
intermediate
Given: <pre>let value: string | null = null;<br>let result = value ?? 'default';</pre> What is the type of <code>result</code>?
The type of result is string because if value is null, the default string is used.
Click to reveal answer
intermediate
Can nullish coalescing be chained with other operators in TypeScript? Give an example.
Yes. Example: <pre>let a: string | null = null;<br>let b: string | undefined = undefined;<br>let c = a ?? b ?? 'fallback';</pre> This returns the first defined and non-null value.
Click to reveal answer
beginner
Why is nullish coalescing useful when working with optional or nullable types in TypeScript?
Because it safely provides a default value only when a variable is null or undefined, avoiding unintended fallback for falsy but valid values like 0 or empty string.
Click to reveal answer
What will let x = 0 ?? 5; assign to x?
Anull
B0
Cundefined
D5
✗ Incorrect
Since 0 is not null or undefined, the nullish coalescing operator returns 0.
Which values cause the right side of ?? to be used?
Anull and undefined
Bfalse and 0
Cempty string and false
Dall falsy values
✗ Incorrect
Only null and undefined trigger the right side value with ??.
What is the type of let y: string | undefined = undefined; let z = y ?? 'hello';?
Anever
Bstring | undefined
Cundefined
Dstring
✗ Incorrect
The result z is always a string because if y is undefined, 'hello' is used.
Which operator should you use to provide a default only when a value is null or undefined?
A??
B||
C&&
D!
✗ Incorrect
The nullish coalescing operator ?? checks specifically for null or undefined.
What will let a = false ?? true; assign to a?
Atrue
Bundefined
Cfalse
Dnull
✗ Incorrect
False is not null or undefined, so the left value is used.
Explain how the nullish coalescing operator (??) works with nullable types in TypeScript.
Think about when you want to keep 0 or false but still provide a fallback for null or undefined.
You got /3 concepts.
Describe the difference between using || and ?? when assigning default values in TypeScript.
Consider what happens if your value is 0 or false.
You got /3 concepts.
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
Step 1: Understand nullish coalescing behavior
The operator ?? checks if the left value is null or undefined.
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.
Final Answer:
Returns the right value only if the left value is null or undefined. -> Option D
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?
The operator ?? is used between two values to provide a fallback if the left is null or undefined.
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.
Final Answer:
const value = input ?? 'default'; -> Option A
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
Step 1: Check the value of name
The variable name is an empty string "", which is NOT null or undefined.
Step 2: Apply nullish coalescing operator
Since name is not null or undefined, name ?? "Guest" returns the left value, which is "" (empty string).
Final Answer:
"" (empty string) -> Option C
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
Step 1: Analyze the value of count
The variable count is 0, which is NOT null or undefined.
Step 2: Apply nullish coalescing operator
Since count is 0 (a valid number), count ?? 10 returns 0, no error occurs.
Final Answer:
No error; output is 0 -> Option B
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
Step 1: Understand the requirement
Return input if it is NOT null or undefined; otherwise, return "No input".
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.
Final Answer:
return input ?? "No input"; -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.