Avoid Mixing Numeric Text and Numbers
Scenario
A booking form sends freeSeats as text "2" instead of integer 2.
Try it yourself
Choose each expression to see how mixing strings and numbers breaks calculations.
| Case | Result |
|---|---|
| "2" + 1 in JavaScript | Select a choice above to see what happens. |
| "10" < "2" (alphabetical text comparison) | Select a choice above to see what happens. |
| Integer 2 + 1 and 10 < 2 after validating integer type | Select a choice above to see what happens. |
Reveal the explanation
- "2" + 1 in JavaScript
Produces text "21" (concatenation) instead of numeric 3.
- "10" < "2" (alphabetical text comparison)
true! Text compares character by character ("1" before "2"), not numerically.
- Integer 2 + 1 and 10 < 2 after validating integer type
Produces 3 and false predictably across Python, JavaScript, and Go.
Validate or convert form text into numbers once at the boundary, and keep internal calculations strictly numeric.
Review first
Your turn
Explain why "60" === 60 is false in strict comparison and write one test case that catches string score input.
Common mistake Relying on loose coercion so "60" silently passes in one language and crashes in Python or Go.
Check your work
Input score = "60" -> rejected with INVALID_SCORE (not silently coerced).
Quick check
This browser could not save your progress. Changes last until you leave the page.