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.

Recent JavaScript Tutorials

Javascript FSO ShortPath property

Javascript FileSystemObject (FSO) provides a ShortPath property that can be accessed using the folder object or file object returned by the GetFolder or GetFile method.

Javascript FSO Path Property

Javascript FileSystemObject (FSO) provides the Path property that can be accessed through the file object or folder object returned by the GetFile or GelFolder method

Javascript FSO Type property

Javascript FileSystemObject (FSO) provides a Type property that can be accessed through file object or folder object returned by the GetFile or GetFolder method

Javascript FSO ShortName property

Javascript FileSystemObject (FSO) provides a ShortName property that can be accessed using the folder object or file object returned by the GetFolder or GetFile method.

Javascript FSO Name Property

Javascript FileSystemObject (FSO) provides a Name property that can be accessed using file object or folder object.