Recall & Review
beginner
What is a default parameter in TypeScript?
A default parameter is a function parameter that has a default value assigned. If the caller does not provide a value, the default is used.
Click to reveal answer
beginner
How do you specify a type for a default parameter in TypeScript?
You write the parameter name, then a colon, then the type, followed by an equals sign and the default value. Example: <code>function greet(name: string = "Guest")</code>Click to reveal answer
intermediate
What happens if you call a function with a default parameter but pass
undefined explicitly?The default value is used because
undefined triggers the default parameter.Click to reveal answer
beginner
Can default parameters be of any type in TypeScript?
Yes, default parameters can be any valid TypeScript type, including primitives, objects, arrays, or even functions.
Click to reveal answer
beginner
Show a simple function with a typed default parameter and explain its parts.
Example: <code>function multiply(x: number, y: number = 2): number { return x * y; }</code><br>Here, <code>y</code> is a number with a default value 2. If <code>y</code> is not passed, it uses 2. The function returns a number.Click to reveal answer
What is the correct way to write a default parameter with type in TypeScript?
✗ Incorrect
Option C correctly declares a parameter 'bar' of type string with a default value 'hello'.
If a function parameter has a default value, what happens when you call the function without that argument?
✗ Incorrect
When a parameter has a default value, calling the function without that argument uses the default.
What type of value triggers the use of a default parameter when passed explicitly?
✗ Incorrect
Passing undefined explicitly triggers the default parameter value to be used.
Can you have multiple default parameters in a TypeScript function?
✗ Incorrect
TypeScript allows multiple parameters to have default values.
What is the return type of this function?
function add(a: number, b: number = 5) { return a + b; }✗ Incorrect
The function returns the sum of two numbers, so the return type is number.
Explain how to declare a function parameter with a default value and a type in TypeScript.
Think about the order: name, colon, type, equals, value.
You got /4 concepts.
Describe what happens when a function with default parameters is called with missing or undefined arguments.
Consider both missing and explicitly undefined arguments.
You got /3 concepts.