OneBite.Dev - Coding blog in a bite size

convert variable from string to float in Javascript

Code snippet on how to convert variable from string to float in Javascript

  const floatString = "3.14";
  const floatValue = parseFloat(floatString);

This code will convert the string variable floatString containing the number “3.14” into a floating point number stored in the variable floatValue.

First, the variable floatString is declared, containing the number “3.14” as a string.

Next, the parseFloat() function is used to convert the content of floatString from a string to a float. This function takes a string as its argument and returns a number.

Finally, the result of the parseFloat() function is stored in the new variable floatValue. This variable now contains the number 3.14 as an actual float, not as a string.

javascript