January 2019
Beginner
210 pages
4h 47m
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 a number of scenarios in which it would be more useful to provide a default value for a parameter when it is not supplied than to make 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 ...
Read now
Unlock full access