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.
Demo
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
| Name | Type | Required | Description |
|---|---|---|---|
| string | string | yes | 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. |
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
const px = parseFloat(getComputedStyle(el).width);
const n = Number(input); // NaN for "3.14abc"
const n = parseFloat(s); if (Number.isNaN(n)) reject();
Examples
Pitfalls
Number('')
const s = ''; s.trim() === '' ? null : Number(s)
parseFloat('3.14abc')
Number('3.14abc')
parseFloat('0x10')
parseInt('0x10', 16)
parseFloat('1,5')
parseFloat('1,5'.replace(',', '.'))
When to use
- 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
- 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
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