0
0
Typescriptprogramming~10 mins

Nullish coalescing with types in Typescript - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to assign a default value using nullish coalescing.

Typescript
const name = userInput [1] "Guest";
Drag options to blanks, or click blank then click option'
A??
B&&
C||
D!
Attempts:
3 left
💡 Hint
Common Mistakes
Using || instead of ??. It treats empty strings or 0 as falsey, which is different.
2fill in blank
medium

Complete the code to safely assign a number or default using nullish coalescing.

Typescript
const count: number = inputCount [1] 10;
Drag options to blanks, or click blank then click option'
A??
B||
C&&
D!
Attempts:
3 left
💡 Hint
Common Mistakes
Using || causes 0 to be replaced by 10, which is incorrect.
3fill in blank
hard

Fix the error in the code by choosing the correct nullish coalescing operator.

Typescript
const result = value [1] fallback;
Drag options to blanks, or click blank then click option'
A|
B||
C&&
D??
Attempts:
3 left
💡 Hint
Common Mistakes
Using || causes unintended fallback for falsey but valid values.
4fill in blank
hard

Fill both blanks to create a typed variable with nullish coalescing default.

Typescript
const message: string = inputMessage [1] [2];
Drag options to blanks, or click blank then click option'
A??
B"No message"
C""
D||
Attempts:
3 left
💡 Hint
Common Mistakes
Using || instead of ??.
Using empty string as default which may be confusing.
5fill in blank
hard

Fill all three blanks to create a typed variable with nullish coalescing and a fallback number.

Typescript
const total: number = (inputTotal [1] [2]) [3] 100;
Drag options to blanks, or click blank then click option'
A??
Bnull
D||
Attempts:
3 left
💡 Hint
Common Mistakes
Using ?? in place of || in the first blank causes no fallback.
Using || in the last blank causes fallback on falsey values.