ECMAScript question detail
Default parameters
Default parameters allow named parameters of a function to be initialized with default values if no value or undefined is passed.
Prior to ES6, you need check for undefined values and provide the default value for undefined values using if/else or ternary operator
function add(a, b) {
a = (typeof a !== 'undefined') ? a : 10;
b = (typeof b !== 'undefined') ? b : 20;
return a + b;
}
add(20); // 40
add(); // 30
In ES6, these checks can be avoided using default parameters
function add(a = 10, b = 20) {
return a + b;
}
add(20); // 40
add(); // 30