0
0
Typescriptprogramming~5 mins

Default parameters with types in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Afunction foo(bar = string "hello")
Bfunction foo(bar: "hello" = string)
Cfunction foo(bar: string = "hello")
Dfunction foo(bar string = "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.
What type of value triggers the use of a default parameter when passed explicitly?
Anull
Bfalse
C0
Dundefined
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.
What is the return type of this function? function add(a: number, b: number = 5) { return a + b; }
Anumber
Bstring
Cvoid
Dany
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.