Number.parseFloat()

The same function as the global parseFloat. Unlike parseInt it understands exponent notation and takes no radix — and unlike Number it tolerates trailing rubbish.

Number static methodES2015 (global since ES1)Live demo
Common call
parseFloat(s)
Returns
a number, or NaN
Replaces
nothing; Number(s) is the strict alternative
Watch out
parseFloat('') is NaN but Number('') is 0
Number.parseFloat(stringstring — The text to parse. Leading whitespace is skipped, parsing stops at the first character that cannot extend the number, and Infinity is recognised by name.type: string · required)
→ number

Demo

Live evaluation
Try:
Inputs
sstringtext to parse
Output
parseFloat('3.14abc')
3.14

Parsing runs from the left and stops at the first character that cannot continue a decimal number, so '3.14abc' gives 3.14 — a CSS length parses neatly. Exponent notation IS understood, which is the main difference from parseInt: '1e3' gives 1000 here and 1 there. The hex case shows there is no radix support at all — parsing stops at the x, leaving 0. The empty string is the detail most worth remembering: parseFloat('') is NaN, while Number('') is 0, so the two disagree on exactly the input a blank form field produces.

Parameters

NameTypeRequiredDescription
stringstringyesThe text to parse. Leading whitespace is skipped, parsing stops at the first character that cannot extend the number, and Infinity is recognised by name.

Return value

number — A number parsed from the start of the string, stopping at the first character that cannot continue a decimal literal. NaN if nothing valid was found.

Common patterns

Read a CSS length
The unit is ignored automatically.
const px = parseFloat(getComputedStyle(el).width);
Strict parsing instead
Number rejects trailing junk.
const n = Number(input);   // NaN for "3.14abc"
Validate the result
Both forms can produce NaN.
const n = parseFloat(s);
if (Number.isNaN(n)) reject();

Examples

1. Trailing junk ignored
parseFloat('3.14abc')
Returns
3.14
2. Leading dot
parseFloat('.5')
Returns
0.5
3. Exponent works
parseFloat('1e3')
Returns
1000
4. parseInt does not
parseInt('1e3', 10)
Returns
1
5. No hex support
parseFloat('0x10')
Returns
0
6. Empty disagrees with Number
[parseFloat(''), Number('')]
Returns
[NaN, 0]

Pitfalls

1. parseFloat('') is NaN while Number('') is 0
The two standard conversions disagree on the single most common invalid input — a blank field. Whichever you use, handle the empty case explicitly rather than relying on the difference.
Silently zero
Number('')
0
Guard blank
const s = '';
s.trim() === '' ? null : Number(s)
null
2. Trailing garbage is accepted
By design — that is what makes it useful for "3.14abc". It also makes it useless for validation, since a field containing 3.14 followed by anything at all passes. Number is the strict counterpart.
Accepts junk
parseFloat('3.14abc')
3.14
Strict
Number('3.14abc')
NaN
3. No radix, and no hex
Unlike parseInt there is no second argument, and a 0x prefix is not recognised. Parsing stops at the x and you get 0 — a plausible-looking wrong answer rather than NaN.
Stops at x
parseFloat('0x10')
0
parseInt for hex
parseInt('0x10', 16)
16
4. It is locale-blind
Only a dot is a decimal separator. A number typed by a European user as 1,5 parses as 1, losing the fraction entirely — and a grouped value like 1,234.5 parses as just 1.
Comma stops it
parseFloat('1,5')
1
Normalise first
parseFloat('1,5'.replace(',', '.'))
1.5

When to use

Use it
  • Extracting a number from text with a unit — CSS lengths, "3.5kg"
  • Values that may be in exponent notation
  • Lenient parsing where trailing content is expected
Reach for something else
  • Validating user input → Number, which is strict
  • Hexadecimal or another base → parseInt with a radix
  • Locale-formatted numbers → normalise the separators first
  • You want a whole number → parseInt, or Math.trunc

Notes

Complexity
O(n) in the length of the parsed prefix
Return
A number, or NaN; nothing is modified
CPython impl
V8: Builtins-number-parsefloat
Memory
May allocate when converting a non-string argument
Thread-safe
Single-threaded

FAQ

Number for validation — it rejects anything not entirely numeric and treats an empty string as 0, which you should guard anyway. parseFloat when the string legitimately has trailing content you want ignored, such as a CSS unit.

Number('3.14px');       // NaN
parseFloat('3.14px');   // 3.14

History

ES1
parseFloat present as a global from the first version.
ES2015
Exposed as Number.parseFloat, the identical function under a namespace.