What is the use of the parseFloat() function in JavaScript?
The parseFloat()
function in JavaScript is used to parse a string and convert it into a floating-point number. It extracts and returns the first numerical value it encounters in the string, ignoring any non-numeric characters that follow.
Syntax:
parseFloat(string)
string
: The string to be parsed and converted into a floating-point number.
Example: Here, the string "3.14"
is passed to the parseFloat()
function, which extracts the numerical value 3.14
from the string and returns it as a floating-point number.
Javascript
const str = "3.14" ; const num = parseFloat(str); console.log(num); // Output: 3.14 |
Output
3.14
Contact Us