Common Bugs with Data Types
Type-coercion and conversion bugs hit every beginner. These five are the most expensive.
From Bee: "I see this exact bug in beginners every week. Read it once, and you'll spot it on yourself before it bites."
Adding a string and a number in JS
js
const total = "5" + 3;
console.log(total); // "53", not 8⚠️
Why this happens
JavaScript coerces 3 to a string and concatenates.✅
The fix
Always parse user input: Number(input) + 3. Or use TypeScript.Form input always being a string
js
const age = document.getElementById("age").value;
if (age > 18) { // sometimes wrong
...
}⚠️
Why this happens
.value is always a string. "5" > 18 is false; "100" > 18 is also false.✅
The fix
Convert: const age = Number(input.value). Validate the result is not NaN.typeof null is "object"
js
console.log(typeof null); // "object" — famous bug⚠️
Why this happens
A historical mistake in JS. Never fixed for backward compatibility.✅
The fix
For null checks, use x === null. For "is this anything," use x != null.Mixing int and float division
py
print(5 / 2) # 2.5 (Python 3)
print(5 // 2) # 2 (integer division)⚠️
Why this happens
/ always returns a float in Python 3. // truncates.✅
The fix
Pick the operator deliberately. In Java, integer / integer truncates by default — use 5.0 / 2 for floats.NaN never equals itself
js
console.log(NaN === NaN); // false!⚠️
Why this happens
NaN is the only value not equal to itself.✅
The fix
Use Number.isNaN(x) or isNaN(x) instead of comparison.💡
Want the full lesson?
Read Data Types — the complete lesson, or test yourself with the quiz.