Default parameters

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 value of "c" instead. The argument c, therefore, will not be undefined. The output of this code will therefore be as follows:

defaultConcat : abc
Note that using a default parameter value will automatically make the parameter that has a default value optional.