Javascript parseFloat function converts the string object into floating point number. If the string value starts from numeric character then it reads the number until it finds a string character. If the first character is of type string then parseFloat function becomes unable to parse the string and returns NaN.
The Javascript parseFloat Function returns only the first starting number of the string type variable. Only space character is allowed as starting character of string variable otherwise parseFloat function could not parse the starting string character and leads to NaN i.e. Not a Number.
Javascript parseFloat Function Syntax
parseFloat(string);
The parseFloat function takes string type object as an argument.
Javascript parseFloat Function Examples
document.write(parseFloat("123") + "<br />");
document.write(parseFloat(" 123") + "<br />");
document.write(parseFloat(" 123 ") + "<br />");
document.write(parseFloat("123.22") + "<br />");
document.write(parseFloat("123a") + "<br />");
document.write(parseFloat("123 a") + "<br />");
document.write(parseFloat("123 456 789") + "<br />");
document.write(parseFloat("number 123") + "<br />");
Note: In above examples of Javascript parseFloat function note the different types of string types values passed to the parseFloat function such as ” 123 ” space character on both ends, “123a” number ends with string character. Notice the output, parseFloat function returned 123 as a number. In the string type input as “number 123” it returned NaN due to string value does not starts with space or number.
Continue to next tutorial: Javascript parseInt Function to learn how to convert string value into numeric integer value.