TypeScript · Junior · Question 13 of 75

How do you declare an optional function parameter?

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.

Further reading

Problem with a question

TypeScript · Junior · question 13

Test yourself

Take the TypeScript · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz