greetUser with three parameters: firstName, lastName, and an optional middleName.middleName parameter as optional using TypeScript syntax.greetUser with three parameters: firstName, lastName, and an optional middleName.middleName parameter as optional using TypeScript syntax.greetUser function with required parametersgreetUser that takes two parameters: firstName and lastName, both of type string. The function should return a string greeting in the format: "Hello, firstName lastName!".Use backticks ` to create a template string for the greeting.
middleName parametergreetUser function to add a third parameter called middleName of type string. Make this parameter optional using TypeScript syntax.Add a question mark ? after middleName to make it optional.
middleName in the greetinggreetUser function to include the middleName in the greeting if it is provided. If middleName is not given, return the greeting with just firstName and lastName. Use an if statement to check if middleName exists.Use an if statement to check if middleName is truthy before including it.
greetUser function twice: once with firstName = "John", middleName = "Paul", and lastName = "Smith"; and once with firstName = "Jane" and lastName = "Doe" only. Print both results using console.log.Remember to call console.log for each greeting.
What does it mean when a function parameter in TypeScript is marked as optional with a ??
? means it is optional and can be skipped when calling the function.Which of the following is the correct way to declare a function with an optional parameter age of type number in TypeScript?
? after the parameter name, before the colon and type.age?: number, which is correct. Options B, C, and D use invalid syntax.paramName?: type [OK]? right after parameter name for optional [OK]? after the type instead of the name? before parameter nameWhat will be the output of this TypeScript code?
function multiply(a: number, b?: number) {
return b ? a * b : a * 2;
}
console.log(multiply(5));
console.log(multiply(5, 3));b is provided, return a * b. Otherwise, return a * 2.multiply(5) means b is undefined, so returns 5 * 2 = 10. Second call: multiply(5, 3) returns 5 * 3 = 15.Identify the error in this TypeScript function declaration:
function logMessage(message?: string, level: string) {
console.log(`[${level}] ${message}`);
}message? is optional but placed before required level. This causes a syntax error.You want to write a function createUser that takes a required username and an optional age. If age is not provided, it should default to 18. Which of these is the best way to declare the function?
age: number = 18, which means if age is omitted, it defaults to 18. function createUser(username: string, age?: number = 18) {} (age?: number = 18) is valid syntax but the ? is redundant since default parameters are already optional. Options B and D do not set default properly and may cause issues.? with default parameter syntaxPass the test and this pattern moves unknown