JavaScript parseInt(), parseFloat() and Number Conversion
Quick answer
Numeric parsers read a valid leading portion of text; Number conversion requires the complete trimmed string to form a valid numeric value (with an empty string becoming zero).
How it works
parseInt(text, radix) accepts an initial integer prefix in base 2–36; use radix 10 for decimal text. parseFloat(text) accepts a decimal floating prefix, including exponent notation. Both first convert to text and can yield NaN. Number recognizes additional complete numeric forms such as 0x hexadecimal.
console.log(parseInt("12px",10), parseFloat("3.5kg"), Number("12px"));Expected output in Node.js:
12 3.5 NaNParsers accept valid prefixes; Number rejects trailing units.
console.log(parseInt("101",2), parseFloat("1e2"), Number(""));Expected output in Node.js:
5 100 0Radix, exponent parsing and empty-string conversion differ.
Common mistakes
Parsing a valid prefix is not full input validation. Use a strict format contract before converting when trailing text is not allowed.
Practice
Compare conversion of unit-labeled input, exponent notation and empty text.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript parseInt(), parseFloat() and Number Conversion — Knowledge Check
Apply the taught concept and predict the output.
Which call interprets the text "ff" as hexadecimal?
What does parseFloat("7.2ms") return?
Why is parseInt unsuitable as the sole strict validation step for decimal text?
What is the exact console output in JavaScript parseInt(), parseFloat() and Number Conversion example 1?
console.log(parseInt("12px",10), parseFloat("3.5kg"), Number("12px"));What is the exact console output in JavaScript parseInt(), parseFloat() and Number Conversion example 2?
console.log(parseInt("101",2), parseFloat("1e2"), Number(""));