What is parseFloat() in JavaScript?
The parseFloat() function takes a string as input, converts it to float, and returns the result.
Syntax
parseFloat(string)
Parameter
The only parameter for this function is a string.
Return value
The function returns a floating-point number that is parsed from the string.
- Only the first number is converted to float and returned if the string has more than one number separated by a space.
- Extra spaces at the beginning and the end are ignored.
- Any alphabets are ignored if they are at the end.
NaNis returned if the first non-whitespace character cannot be turned to float.
Code
console.log( parseFloat("56.019") )console.log( parseFloat(" 56.019 abc") )console.log( parseFloat("57") )console.log( parseFloat("68.9 55") )console.log( parseFloat("abc 56.019") )