greet with a parameter name of type stringname to "Guest"name parameter"Alice"greet with a parameter name of type stringname to "Guest"name parameter"Alice"greet function with a typed name parametergreet that takes one parameter named name of type string. Do not add a default value yet.Use function greet(name: string) to define the function and return a greeting string using backticks.
"Guest" to the name parametergreet function so that the name parameter has a default value of "Guest".Add = "Guest" after the parameter name: string to set the default.
greet twice: once without arguments and once with "Alice"greet() without arguments and store the result in greeting1, then call greet("Alice") and store the result in greeting2.Use const greeting1 = greet() and const greeting2 = greet("Alice") to call the function.
greeting1 and greeting2console.log statements to print greeting1 and greeting2.Use console.log(greeting1) and console.log(greeting2) to show the greetings.
What is the main purpose of default parameters in TypeScript functions?
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; }param: type = default.y: number = 2. Others have syntax errors or wrong order.What is the output of the following TypeScript code?
function greet(name: string = "Guest") {
return `Hello, ${name}!`;
}
console.log(greet());
console.log(greet("Alice"));greet() without argument uses default "Guest".Identify the error in this TypeScript function and choose the correct fix:
function calculateArea(width: number = 5, height: number) {
return width * height;
}width with default to the end fixes the error.Consider this function that merges two objects with default parameters:
function mergeOptions(options: { verbose?: boolean } = { verbose: false }, defaults: { verbose: boolean } = { verbose: true }) {
return { ...defaults, ...options };
}
console.log(mergeOptions());
console.log(mergeOptions({ verbose: true }));What is the output of the two console.log calls?
options and defaults have default objects with verbose flags.{ ...defaults, ...options } merges with options overriding defaults.options = { verbose: false }, defaults = { verbose: true }, merged result is { verbose: false } because options overwrites defaults.{ verbose: true } as options, so merged result is { verbose: true }.Pass the test and this pattern moves unknown