February 2019
Beginner
694 pages
18h 4m
English
A subtle variant of the optional parameter syntax allows us to specify the default value of a parameter, if it is not supplied. Let's modify our preceding function definition to use an optional parameter with a default value, as follows:
function concatStringsDefault(
a: string,
b: string,
c: string = "c") {
return a + b + c;
}
var defaultConcat = concatStringsDefault("a", "b");
console.log(`defaultConcat : ${defaultConcat}`);
This function definition has now dropped the ? optional parameter syntax, but instead has assigned a value of "c" to the last parameter, c:string = "c". By using default parameters, if we do not supply a value for the parameter named c, the concatStringsDefault function will substitute the default ...
Read now
Unlock full access