WeCodeAcademy

Optional, Default and Rest Parameters

Mark a parameter optional with ?, give it a default with =, or collect extras with ...rest.

Exampletypescript
Open in editor »

An optional parameter can be undefined, so TypeScript makes you handle that before using it as a plain value.

Exampletypescript
Open in editor »
Quick check

What does `...nums: number[]` allow?

Your turn: write the code

Write greet(name: string, greeting = "Hello"): string returning greeting, name. Print greet("Ada") and greet("Sam", "Hi").

Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.

Tip: use the left and right arrow keys to move between lessons.