Coercion & explicit conversion
Understand the idea
A quantity field contains 2, but adding one produces 21. Input values are strings, and + can join strings.
Choose the adapter deliberately
A browser form gives you text even when the person typed digits. Your code needs a number for a quantity calculation and text again for a label. Conversion exists because these boundaries are normal, not because the user entered something wrong.
Why form input surprises people
Start by inspecting the type you actually received. A numeric-looking input is still a string when read through an input element’s value . If a calculation produces unexpected text, inspect the values before the operator instead of assuming the operator itself is broken.
The + operator can add numbers or join strings. With the text "2" on one side, + 1 produces "21". A subtraction such as "2" - 1 instead asks for numeric conversion. That inconsistency is why converting once at the input boundary is easier to read than hoping each operator does the right thing.
| Choice | What it does | A useful warning |
|---|---|---|
| Number(text) | Converts the entire value to a number | An empty or whitespace-only string becomes 0 |
| parseFloat(text) | Reads a numeric prefix | "12px" becomes 12, which may hide bad input |
| String(value) | Explicitly produces text | Useful at a display boundary |
| Boolean(value) | Uses truthiness rules | The non-empty string "false" becomes true |
| === and !== | Compare without type coercion | Usually the clearest default |
| == and != | May convert types while comparing | Different types can compare equal |
Turning text into numbers
Number asks whether the whole input can represent a number. parseFloat reads a numeric prefix, so the text "12px" becomes 12. That can be useful when reading a known formatted value, but wrong when validating a quantity field. Choose deliberately whether extra characters are acceptable.
Choosing an equality rule
Strict equality, ===, compares without converting the operands to a common type. Loose equality, ==, applies conversion rules for different types. Prefer strict equality for ordinary comparisons and convert deliberately before comparing. Neither operator compares separate by their contents.
Validate before calculating
Conversion is not validation. Number("") is zero, while Number("two") is NaN. Check whether input is blank before conversion if blank is invalid, then use Number.isFinite for a finite numeric value or Number.isNaN when specifically checking . is another conversion: the string "false" is truthy because it is not empty.
Decide what counts as acceptable input before converting it. For a quantity, you might require non-empty text, a finite result, an integer, and a value at least zero. Number.isFinite rules out both NaN and Infinity. Conversion answers “what number does this become?”; validation answers “is that number allowed here?”
Mental model
FOLLOW THE VALUE
Without conversion, addition joins text.
01 / 03Trace the transformation
Live example
The first log is arithmetic after conversion. The next two compare the same string and number under different equality rules.
Your console output appears here.
Guided challenge
Add numerically
Convert the raw quantity before adding one. Preserve the two comparison logs so you can see their different rules.
Your console output appears here.
EXPECTED CONSOLE OUTPUT
3 false true
Hints 0/2
Use all hints or make 3 failed runs (0/3).
Free sandbox
Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.
Your console output appears here.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 100 XP. You can always revisit it.