JavaScript Code


How to convert string to float in JavaScript?


Convert string to float in JavaScript

In this tutorial, you are given a string value. You should convert the string to floating point number.

Solution

To convert a given string into a floating point number, use JavaScript global function parseFloat(). Pass the string as argument to parseFloat() function, and the function returns the floating point value.

Program

str is the given string value. We use parseFloat() method to parse the string str into a float.

let str = "3.14";
let num = parseFloat(str);
console.log(num);
console.log(typeof(num));