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?
Afunction foo(bar = string "hello")
Bfunction foo(bar: "hello" = string)
Cfunction foo(bar: string = "hello")
Dfunction foo(bar string = "hello")
✗ 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?
AThe parameter is undefined.
BThe default value is used.
CAn error is thrown.
DThe function ignores the parameter.
✗ 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?
Anull
Bfalse
C0
Dundefined
✗ Incorrect
Passing undefined explicitly triggers the default parameter value to be used.
Can you have multiple default parameters in a TypeScript function?
AYes, any number of parameters can have defaults.
BNo, only one default parameter is allowed.
CYes, but only if they are at the end.
DNo, default parameters are not supported.
✗ 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; }
Anumber
Bstring
Cvoid
Dany
✗ 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.
Practice
(1/5)
1.
What is the main purpose of default parameters in TypeScript functions?
easy
A. To provide fallback values when arguments are missing
B. To enforce strict typing on function return values
C. To declare variables inside functions
D. To create anonymous functions
Solution
Step 1: Understand default parameters
Default parameters give a value to a function argument if no value is passed.
Step 2: Identify the purpose
This helps avoid errors when arguments are missing by providing fallback values.
Final Answer:
To provide fallback values when arguments are missing -> Option A
Quick Check:
Default parameters = fallback values [OK]
Hint: Default parameters fill missing arguments automatically [OK]
Common Mistakes:
Confusing default parameters with return type enforcement
Thinking default parameters declare variables
Mixing default parameters with anonymous functions
2.
Which of the following is the correct syntax for a TypeScript function with a default parameter of type number?
function multiply(x: number, y: number = 2) { return x * y; }
easy
A. function multiply(x: number, y = 2: number) { return x * y; }
B. function multiply(x = 2: number, y: number) { return x * y; }
C. function multiply(x: number, y: number = 2) { return x * y; }
D. function multiply(x:, y: number = 2) { return x * y; }
Solution
Step 1: Check parameter type and default value syntax
In TypeScript, the type annotation comes before the default value: param: type = default.
Step 2: Verify each option
function multiply(x: number, y: number = 2) { return x * y; } correctly uses y: number = 2. Others have syntax errors or wrong order.
Final Answer:
function multiply(x: number, y: number = 2) { return x * y; } -> Option C
Quick Check:
Type before default value = correct syntax [OK]
Hint: Type annotation before default value: param: type = value [OK]
Common Mistakes:
Placing default value before type annotation
Omitting type annotation on default parameter
Assigning default to wrong parameter
3.
What is the output of the following TypeScript code?