function calcPrice (price, tax, discount) { tax = tax || 0.05 discount = discount || 0 return // math }
function calcPrice (price, tax, discount) { tax = tax || 0.05 discount = discount || 0 return // math } calcPrice(10, 0, .1)
The || operator checks for falsy values and 0 is a falsy value. To fix this, instead of checking for a falsy value, you can check for undefined or just use ES6’s Default Parameters.
// es5 function calcPrice (price, tax, discount) { tax = typeof tax === 'undefined' ? 0.05 : tax discount = typeof discount === 'undefined' ? 0 : discount return // math }
or using ES6’s default parameters
function calcPrice (price, tax = 0.05, discount = 0) { return // math }