parseFloat Function

Top  Previous  Next

Parses a string into a number.




string A string to be parsed and converted to a number.

Return Value

A floating point number representing the value of the parsed string, or NaN (not a number) if string does not begin with a valid number.


parseFloat stops parsing if it encounters a character that is not a valid part of a number. If string starts with a valid number, parseFloat will return that number, ignoring extra characters. If string does not start with a valid number, parseFloat returns NaN. You can test for NaN using isNaN.


parseFloat("1.5cm") // returns 1.5
parseFloat("xyz") // returns NaN

See Also

isNaN, parseInt, parseNumber