easyLogic ErrorsJavaScript
The silent NaN
A developer asked an AI assistant to calculate a cart line total from a quantity field. The suggested code passes every manual test where a number is typed, but when a customer clears the field before checkout, the page silently shows a broken price instead of stopping them.
cart.js
function calculateLineTotal(price, quantityInput) {
const quantity = parseInt(quantityInput);
return price * quantity;
}
const total = calculateLineTotal(499, ''); // customer cleared the field
renderTotal(total);What does renderTotal(total) end up displaying, and why does nobody notice?
'₹NaN' — parseInt('') returns NaN, and NaN silently propagates through the multiplication with no error thrown.
'₹0' — parseInt('') defaults to 0, so the total is simply zero.
'₹499' — an empty quantity is treated as a quantity of 1.
The function throws a TypeError before renderTotal is ever called.