Challenge - 5 Problems
Null and Undefined Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of function with null and undefined
What is the output of this TypeScript code?
Typescript
function checkValue(x: number | null | undefined) { if (x === null) return "Null"; if (x === undefined) return "Undefined"; return x * 2; } console.log(checkValue(null)); console.log(checkValue(undefined)); console.log(checkValue(5));
Attempts:
2 left
💡 Hint
Check how the function compares the input to null and undefined explicitly.
✗ Incorrect
The function returns "Null" if the input is exactly null, "Undefined" if exactly undefined, otherwise doubles the number. So the outputs are "Null", "Undefined", and 10.
❓ Predict Output
intermediate1:30remaining
Value of variable after assignment
What is the value of variable
result after running this code?Typescript
let value: string | null | undefined = undefined; value = value ?? "default"; const result = value;
Attempts:
2 left
💡 Hint
The nullish coalescing operator (??) returns the right side if the left side is null or undefined.
✗ Incorrect
Since value is initially undefined, value ?? "default" evaluates to "default". So result is "default".
🔧 Debug
advanced2:00remaining
Why does this code cause an error?
This TypeScript code causes a compile-time error. What is the reason?
Typescript
function greet(name: string | null) { console.log("Hello, " + name.toUpperCase()); } greet(null);
Attempts:
2 left
💡 Hint
Think about what happens if name is null and you call a method on it.
✗ Incorrect
The parameter name can be null, but the code calls name.toUpperCase() without checking. This causes a compile-time error because null has no methods.
🧠 Conceptual
advanced1:30remaining
Difference between null and undefined in TypeScript
Which statement correctly describes the difference between
null and undefined in TypeScript?Attempts:
2 left
💡 Hint
Think about what happens when you declare a variable but don't assign it, versus assigning null explicitly.
✗ Incorrect
In TypeScript, undefined means a variable has not been assigned a value yet, while null is an explicit assignment meaning 'no value'.
❓ Predict Output
expert2:30remaining
Output of complex null and undefined checks
What is the output of this TypeScript code?
Typescript
function test(x: string | null | undefined) { switch (x) { case null: return "Null case"; case undefined: return "Undefined case"; default: return x.length; } } console.log(test(null)); console.log(test(undefined)); console.log(test("hello"));
Attempts:
2 left
💡 Hint
Remember how switch compares values and how string length works.
✗ Incorrect
The switch matches null and undefined cases exactly, returning strings. For "hello", it returns length 5.