0
0
Typescriptprogramming~20 mins

Nullish coalescing with types in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Nullish Coalescing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this code using nullish coalescing?
Consider the following TypeScript code snippet. What will be logged to the console?
Typescript
const value: string | null = null;
const result = value ?? "default";
console.log(result);
A"default"
Bnull
Cundefined
DTypeError
Attempts:
2 left
💡 Hint
Remember that nullish coalescing returns the right side only if the left side is null or undefined.
Predict Output
intermediate
2:00remaining
What is the output when using nullish coalescing with undefined?
Analyze this TypeScript code. What will be printed?
Typescript
let input: number | undefined = undefined;
const output = input ?? 42;
console.log(output);
Aundefined
B0
C42
DTypeError
Attempts:
2 left
💡 Hint
Nullish coalescing treats undefined as nullish and returns the right side.
Predict Output
advanced
2:00remaining
What is the output when nullish coalescing is combined with a falsy value 0?
Look at this TypeScript code. What will be logged to the console?
Typescript
const count: number | null = 0;
const result = count ?? 10;
console.log(result);
Aundefined
B0
Cnull
D10
Attempts:
2 left
💡 Hint
Nullish coalescing only treats null or undefined as nullish, not other falsy values like 0.
Predict Output
advanced
2:00remaining
What is the type of the variable after nullish coalescing with union types?
Given this TypeScript code, what is the type of 'finalValue'?
Typescript
type Input = string | null | undefined;
const input: Input = null;
const finalValue = input ?? "fallback";
Anever
Bstring | null
Cstring | null | undefined
Dstring
Attempts:
2 left
💡 Hint
Nullish coalescing narrows the type by removing null and undefined from the left side.
🧠 Conceptual
expert
2:00remaining
Which option correctly explains the difference between || and ?? operators in TypeScript?
Choose the best explanation about how the logical OR (||) operator differs from the nullish coalescing (??) operator in TypeScript.
A|| returns the right side if the left side is falsy (like 0, '', null, undefined), while ?? returns the right side only if the left side is null or undefined.
BBoth || and ?? behave the same and return the right side if the left side is falsy.
C|| returns the right side only if the left side is null or undefined, while ?? returns the right side if the left side is falsy.
D|| and ?? are interchangeable and have no difference in behavior.
Attempts:
2 left
💡 Hint
Think about how 0 or empty string are treated by each operator.