What is type coercion — and why your cart says "10050" instead of 150

Here's a classic. You asked an AI to build a cart. The item costs 100, delivery is 50. The screen says: "Total: 10050".
The code didn't crash. Not a single error in the console. JavaScript honestly added what you gave it. It just added text, not numbers.
That's type coercion. Five minutes from now you'll spot it in code at a glance.
What type coercion is
Type coercion is when a language converts a value of one type into another on its own. Without you asking.
Every value has a type: a number, a string, a boolean true/false, and others. Operations expect specific types. Adding numbers expects numbers. An if expects a yes or a no.
When the wrong thing arrives, a language has two options:
- Fail with an error. That's Python:
"5" + 2gives aTypeErrorright away. - Quietly convert and carry on. That's JavaScript. It tries to guess what you meant.
The second path is called implicit coercion. Handy while it guesses right. Painful when it doesn't.
Why "5" + 2 = "52", but "5" - 2 = 3
Here's the one thing worth remembering.
In JavaScript, plus has two jobs: adding numbers and gluing strings. If either side is a string, plus picks gluing. The number turns into text and gets stuck on.
Minus has only one job — subtracting. It doesn't glue strings. So it turns the string into a number and does the math.
"5" + 2 // "52" — gluing
"5" - 2 // 3 — subtraction
"5" * 2 // 10 — multiplication does math too
That's your cart. A value from an input field is always a string. Even if the field is type="number". Even if it says "100". The user typed "100", you added 50 — and got "10050".
Comparing strings has its own rules too. "10" < "9" is true. Strings are compared letter by letter, like words in a dictionary. And "1" comes before "9".
Where else coercion hides
Not just in plus. Here's where it fires most often.
Conditions. An if turns anything into true or false. An empty string is false. But the string "0" is true, even though there's a zero inside. An empty array [] is true as well.
Loose equality ==. It first coerces both sides to one type, then compares. Hence the weirdness:
"0" == false // true
null == undefined // true
null == 0 // false
null >= 0 // true — yes, really
The last two lines aren't a typo. == and >= coerce null by different rules. You don't need to memorize this. You just need to stop using ==.
Empty string to number. Number("") and Number(" ") both give 0. An empty "Quantity" field quietly becomes an order of zero. And +"12px" gives NaN — and then NaN spreads through every calculation.
How to stop catching these bugs
Good news: you don't need to learn the coercion table. Three habits are enough.
- Always
===instead of==. Strict equality doesn't coerce. Different types mean not equal. No surprises. - Convert to a number yourself, right at the input. Grabbed a value from a field — immediately do
Number(value). Commas and spaces are covered in detail in how to convert a string to a number. - Check the result. After converting —
Number.isNaN(x). If it'strue, show an error instead of calculating further.
Here's what that looks like in the cart:
const price = Number(priceInput.value)
const delivery = 50
if (Number.isNaN(price)) {
showError("Enter the price as a number")
} else {
total.textContent = price + delivery // 150
}
Number fields have a shortcut: input.valueAsNumber. It hands you a number right away, or NaN if the field is empty.
Why this matters if AI writes your code
AI writes code fast, but it often forgets about types. Especially in forms, URL parameters and data from localStorage — everything arrives there as strings.
So it helps to know where to look. See "glued" numbers like "10050" or "1510" in the output — that's plus with a string. See a condition firing "at the wrong time" — check for == or a "0" string.
And one line in your prompt removes half the problems:
Build a cart that adds up the totalBuild a cart that adds up the total. Convert every value from input fields to a number right away with Number(). If the result is NaN, show an error under the field and stop calculating. Compare only with ===, never ==.Is type coercion a bug in the language?
No, it was a deliberate choice: browsers tried not to crash on messy data. But many consider it a poor one. That's part of why TypeScript exists — it catches these spots before the code even runs.
What's the difference between explicit and implicit coercion?
Explicit is when you write Number(x) or String(x) yourself. Implicit is when the language does it for you inside +, == or if. Explicit reads clearly and doesn't surprise anyone.
Is it the same in other languages?
Depends on the language. Python and Ruby throw an error on "5" + 2. PHP used to coerce even more aggressively than JavaScript and only made string-to-number comparisons saner in version 8.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





