Last updated on March 10, 2023
The parseFloat() method converts the specified value into the floating-point number. It has a required parameter in which it accepts string values.
Syntax
parseFloat(string)
Parameters
It has one required parameter in which it accepts string values.
Return Value
It returns a floating-point number. In case, the method doesn’t convert the first character then it returns NaN (Not-a-Number).
Usage
Let’s use the parseFloat() method by passing different values as an argument.
parseFloat(5);
// Output: 5
// Data Type: Number
In the above example, we passed an integer value 5 to the parseFloat() method that returned the same value with the number data type.
parseFloat("50");
// Output: 50
// Data Type: Number
In this example, we passed a string value of 50 and it returned the same value with the number data type.
parseFloat("50.10");
// Output: 50.1
// Data Type: Number
Above, we passed a string that contains a decimal number to the parseFloat() method that converted it into a floating-point number.
parseFloat("a 50.10");
// Output: NaN
In this example, we added an alphabetic letter to the previous string value and passed it to the method that returned the NaN (Not a Number).