Optional, Default and Rest Parameters
Mark optional parameters with ?, give defaults with =, and collect extra arguments with ...rest. Optional parameters must come after required ones.
Example
function greet(name: string, greeting = "Hello", punct?: string, ...tags: string[]) {
return `${greeting}, ${name}${punct ?? "!"} ${tags.join("|")}`;
}
console.log(greet("Ada"));
console.log(greet("Tunde", "Hi", "?", "a", "b"));Result
Hello, Ada! Hi, Tunde? a|b
Exercise
Collect extra arguments.
function f(nums: number[]) {}
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.