Open In App

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)

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.




const str = "3.14";
const num = parseFloat(str);
console.log(num); // Output: 3.14

Output
3.14

Article Tags :