Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the variable is a string using typeof.
Typescript
if (typeof value === [1]) { console.log("It's a string!"); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'string' without quotes.
Comparing to 'String' with uppercase S.
Using typeof value === string (missing quotes).
✗ Incorrect
The typeof operator returns "string" for string values, so we compare with "string".
2fill in blank
mediumComplete the code to check if the variable is a number using typeof.
Typescript
if (typeof num === [1]) { console.log("It's a number!"); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Number' with uppercase N.
Comparing to 'num' instead of 'number'.
Missing quotes around 'number'.
✗ Incorrect
typeof returns "number" for numeric values, so we compare with "number".
3fill in blank
hardFix the error in the typeof check to correctly identify a boolean.
Typescript
if (typeof flag === [1]) { console.log("It's a boolean!"); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase "Boolean" instead of lowercase.
Using "bool" which is not a valid typeof result.
Missing quotes around the type string.
✗ Incorrect
The typeof operator returns "boolean" for boolean values, all lowercase.
4fill in blank
hardFill both blanks to create a type guard that checks if input is a string or a number.
Typescript
if (typeof input === [1] || typeof input === [2]) { console.log("Input is string or number"); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect type strings like "bool" or "object".
Using && instead of || which requires both types at once.
Missing quotes around type strings.
✗ Incorrect
We check if input is either "string" or "number" using typeof comparisons.
5fill in blank
hardFill all three blanks to create a function that returns the type of the value as a string using typeof.
Typescript
function getType(value: unknown): string {
return typeof [1] [2] [3];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' for concatenation.
Not using quotes around the empty string.
Using a variable name other than 'value'.
✗ Incorrect
The function returns typeof value concatenated with an empty string to ensure the return type is string.