0
0
Typescriptprogramming~10 mins

Default parameters 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 set a default value for the parameter name.

Typescript
function greet(name: string = [1]) {
  return `Hello, ${name}!`;
}
Drag options to blanks, or click blank then click option'
Anull
B"Guest"
Cundefined
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using null or undefined as default for a string parameter.
Not using quotes around the default string value.
2fill in blank
medium

Complete the function to set a default number parameter with type.

Typescript
function multiply(x: number, y: number = [1]): number {
  return x * y;
}
Drag options to blanks, or click blank then click option'
Anull
B"2"
C2
Dundefined
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string "2" instead of number 2.
Using null or undefined as default for a number parameter.
3fill in blank
hard

Fix the error by completing the default parameter with the correct type.

Typescript
function setFlag(flag: boolean = [1]): string {
  return flag ? "On" : "Off";
}
Drag options to blanks, or click blank then click option'
Atrue
B"true"
C1
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Using string "true" instead of boolean true.
Using number 1 or null as default for boolean.
4fill in blank
hard

Fill both blanks to create a function with a default string parameter and return type.

Typescript
function welcome(message: [1] = [2]): string {
  return message;
}
Drag options to blanks, or click blank then click option'
Astring
B"Hello"
Cnumber
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using number or boolean as type for a string parameter.
Not quoting the default string value.
5fill in blank
hard

Fill all three blanks to define a function with a default boolean parameter and a typed return.

Typescript
function isActive(status: [1] = [2]): [3] {
  return status;
}
Drag options to blanks, or click blank then click option'
Astring
Btrue
Cboolean
D"active"
Attempts:
3 left
💡 Hint
Common Mistakes
Using string type or string default for boolean parameter.
Mismatching return type with parameter type.