parseFloat
Reads a floating-point number from the start of a string.
Available since: ES1
Syntax
parseFloat(string)Parameters
| Parameter | Type | Description |
|---|---|---|
| string | string | A string that starts with a number. |
Return value
numberA floating-point number, or NaN if the string does not start with a number.
Examples
A number with units:
console.log(parseFloat('3.75kg'));
console.log(parseFloat('12px'));Output:
3.75
12When parsing fails:
console.log(parseFloat('kg 3.75'));
console.log(parseFloat('3,75'));Output:
NaN
3Notes
The decimal separator must be a dot: a comma ends the number. If the string must contain nothing but a number, Number() is stricter: Number('12px') gives NaN.
See also
- parseInt — Converts a string to an integer.
- Number() — Converts a value to a number.
- Number.isNaN — Checks that a value is NaN, without type conversion.
Related recipes
In the tutorial
- Type conversion — How to turn a string into a number and back in JavaScript — Number, String, Boolean, parseInt and implicit conversion.