TypeScript · Junior · Question 13 of 75
How do you declare an optional function parameter?
- ✓Correct answer:
function greet(name?: string) - 2
function greet(optional name: string) - 3
function greet(name: string = ?)
Explanation
? makes the parameter optional; its type is string | undefined. The alternative is a default value: greet(name = 'Guest'), in which case the type is inferred as string. Optional parameters must come after required ones.