To convert a string to float in JavaScript, you can use the 'parseFloat()' function.
In this example,
str1
with a value of "3.14"
.parseFloat()
function to convert the string to a float and store the result in a variable float1
.float1
to the console.// Convert "3.14" String to Float
let str1 = "3.14";
let float1 = parseFloat(str1);
console.log("Float value is: " + float1);
Float value is: 3.14
In this example,
str2
with a value of "10.5"
.parseFloat()
function to convert the string to a float and store the result in a variable float2
.float2
to the console.// Convert "10.5" String to Float
let str2 = "10.5";
let float2 = parseFloat(str2);
console.log("Float value is: " + float2);
Float value is: 10.5
In this tutorial, we learned How to convert String to Float in JavaScript language with well detailed examples.