A payment check such as `if (!transactionId)` can treat numeric zero as missing and charge a customer twice. JavaScript silently converts values in conditions, arithmetic, comparisons, templates, and built-in methods, so the visible bug may appear far from the conversion that caused it. section 4.0 section 4.4.2
Conversion follows operator-specific rules rather than one universal cast. Minus, multiplication, and division convert operands to numbers; plus prefers string concatenation; loose equality converts aggressively; strict equality performs no conversion. Conditions use truthy and falsy rules, while template literals convert interpolated values to strings. section 4.4.2 section 4.2.1
Useful rules for predicting JavaScript data conversion include:
- Treat `undefined`, `null`, `false`, numeric zero, `NaN`, and the empty string as falsy; treat strings such as `'0'`, arrays, and objects as truthy. section 4.2.1
- Expect `'5' - 2` to produce `3`, but `'5' + 2` to produce `'52'`. section 4.4.2
- Use strict equality when implicit conversion is undesirable; `5 == '5'` is true, while strict equality never coerces. section 4.4.2
- Convert values once at system boundaries instead of repeatedly inside loops, comparisons, or sorting operations. section 4.4.4 section 4.6
Arrays reveal multi-step conversion
`Number([1])` produces `1`, whereas `Number([1, 2])` produces `NaN`. The array is first converted to a string: `[1]` becomes `'1'`, while `[1, 2]` becomes a multi-value string that cannot become a number. section 4.4.4
function processPayment(amount, discountCode, isPriority) {
if (!discountCode) {
discountCode = 'NONE';
}
const finalAmount = amount - (discountCode * 0.01 * amount);
if (isPriority == true) {
return expeditedCharge(finalAmount);
}
return standardCharge(finalAmount);
}
// All broken in different ways:
processPayment(100, '0', false); #A
processPayment(100, '', false); #B
processPayment(100, '50', 'yes'); #CInspect the payment function to see three independent conversion failures: falsy handling, arithmetic coercion, and loose equality with a non-boolean string. section 4.0





















