April 2018
Beginner
536 pages
13h 21m
English
When a function has some optional parameters, we must check whether an argument has been passed to the function (just like we did in the previous example) to prevent potential errors.
There are some scenarios where it would be more useful to provide a default value for a parameter when it is not supplied than making it an optional parameter. Let's rewrite the add function (from the previous section) using the inline if structure:
function add(foo: number, bar: number, foobar?: number): number {
return foo + bar + (foobar !== undefined ? foobar : 0);
}
There is nothing wrong with the preceding function, but we can improve its readability by providing a default value for the foobar parameter instead of using ...