parseFloat()
The global parseFloat() function reads a decimal number from the start of a string and returns it as a number, or NaN when no number starts the string. Parsing stops at the first character that can’t continue the number, so trailing text is ignored:
> parseFloat("3.14 kg")
3.14
> parseFloat("kg 3.14")
NaN
parseFloat() is an ECMAScript built-in. The static method Number.parseFloat() is the same function object, so the two always give the same result.
Syntax
The parseFloat() function takes a single argument:
parseFloat(<string>)
| Parameter | Description |
|---|---|
string |
The value to parse. A non-string value is converted to a string first, and leading whitespace is ignored |
The function isn’t a constructor, so calling it with new throws a TypeError. It has no radix parameter and always reads base-10 digits. For other bases, parseInt() takes a radix.
Return Value
The result is a number built from the longest prefix that forms a decimal literal. That prefix can have a leading + or -, digits with one decimal point, and an exponent such as e-3. The prefix Infinity is accepted too.
The result is NaN when the string, after leading whitespace, doesn’t start with a valid number. A value too large for a number becomes Infinity or -Infinity.
Type a string or pick an example to see which characters parseFloat() reads and where it stops:
Examples
A fitness app stores weights typed as "72.5kg" and needs them in grams. Parsing stops at k, so the unit drops away and the value is ready for arithmetic:
> parseFloat("72.5kg") * 1000
72500
A lab instrument exports readings in exponent notation with a unit, such as "6.02e23 mol". The exponent is part of the decimal literal, so parseFloat() keeps it:
> parseFloat("6.02e23 mol")
6.02e+23
> parseFloat(" -1.5E-3 V")
-0.0015
A form field accepts a price, and the code needs to catch input that isn’t a number at all. The result is NaN, which Number.isNaN() detects:
> Number.isNaN(parseFloat("free"))
true
> Number.isNaN(parseFloat("19.99"))
false
Common Gotchas
A thousands separator or a decimal comma ends the parse, so formatted numbers lose everything after the first comma. Numeric separators like 1_000 aren’t accepted in strings either:
> parseFloat("1,299.99")
1
> parseFloat("3,5")
3
> parseFloat("1_000.5")
1
The function reads only base-10 digits. A hexadecimal string stops after the leading 0, so the result is 0 instead of NaN:
> parseFloat("0x1A")
0
A partly valid string still returns a number, with no sign that the rest was ignored. Input like "12.5.3" comes back as 12.5. For an all-or-nothing conversion, Number() returns NaN instead:
> parseFloat("12.5.3")
12.5
> Number("12.5.3")
NaN
Related Resources
Course
JavaScript Numbers for Pythonistas
Port a coffee roastery's batch planner from Python to JavaScript, and learn to divide, round, read numbers from text, and format them with JavaScript's one number type.
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Nov. 14, 2026