function calcPrice(price, tax, discount) { tax = tax || 0.05; discount = discount || 0; return price - price * discount + price * tax; }
The || operator checks for falsy values and 0 is a falsy value, which would accidentally apply the default tax rate. To fix this, instead of checking for a falsy value, you can use the nullish coalescing (??) operator.
function calcPrice(price, tax, discount) { tax = tax ?? 0.05; discount = discount ?? 0; return price - price * discount + price * tax; }
The nullish coalescing operator works because it returns its right-hand side operand when its left-hand side operand is null or undefined (NOT 0), and otherwise returns its left-hand side operand.