Mr. Tono’s rice weigh note read by three people: Mrs. Tini’s parseInt, Rara’s parseFloat and Rizal’s Number. Where does each stop reading, and when does NaN…
Why it matters
Turning text into a number happens in every form, every CSS value, every note from a machine. Pick the wrong one and '12kg' quietly becomes NaN, or dirty input slips through as 12, or an empty note becomes 0.
The key idea
parseInt reads part of it, Number all of it or nothing
Common mistakes
Old notes ("0x..", "08") read differently than expected With no radix, parseInt reads 0x as hexadecimal. Always give the radix: parseInt(x, 10); for hexadecimal, parseInt(x, 16).
Empty text passes validation and becomes 0Number('') and Number(' ') are 0, not NaN. Check for empty first (x.trim() === '') before converting.
"12abc" passes a "must be only a number" check as 12 parseInt only reads part of it. For "must be only a number", use Number + Number.isFinite, not parseInt.
Try it in this playground
Try an empty note Number("") is 0, parseInt("") is NaN. A validation trap: empty becomes zero.
Try '12,5' with a comma Every reader stops at the comma: parseFloat gives 12, Number gives NaN.
Try '1e3' and '0x1F' Number knows exponents (1000) and hexadecimal (31); parseInt with radix 10 does not.
Switch parseInt’s radix to 16 '1F' becomes 31, '12kg' becomes 18: hexadecimal digits go up to f.