DEFAULT PARAMETER VALUES
In ECMAScript 5.1 and before, a common strategy for implementing default parameter values was to determine if a parameter was not provided to the function invocation by checking if it was undefined, and assigning a value to the parameter if that was the case:
function makeKing(name) {
name = (typeof name !== 'undefined') ? name : 'Henry';
return `King ${name} VIII`;
}
console.log(makeKing()); // 'King Henry VIII'
console.log(makeKing('Louis')); // 'King Louis VIII'
This is no longer required in ECMAScript 6, as it supports explicitly defining values for parameters if they are not provided when the function is invoked. The equivalent of the previous function with ES6 default parameters is done using the = operator directly inside the function signature:
function makeKing(name = 'Henry') {
return `King ${name} VIII`;
}
console.log(makeKing('Louis')); // 'King Louis VIII&apos...