JavaScript(JS) built-in method - parseFloat

https://‮figi.www‬tidea.com

The parseFloat() method in JavaScript is a built-in function that parses a string argument and returns a floating point number. The function tries to convert the string to a number, starting from the beginning of the string. If it encounters an invalid character or a decimal point more than once, it stops and returns the parsed value up to that point.

Here's an example of using the parseFloat() method:

parseFloat("3.14"); // 3.14
parseFloat("314e-2"); // 3.14
parseFloat("3.14someletters"); // 3.14
parseFloat("hello"); // NaN

In the example above, we use the parseFloat() method to parse different strings. The first call to parseFloat() returns the floating point number 3.14, which is the beginning of the string "3.14". The second call returns the same value, since "314e-2" is also a valid floating point number. The third call returns 3.14, even though the string contains additional letters, because the function stops parsing as soon as it encounters an invalid character. The fourth call returns NaN, because the string "hello" cannot be parsed as a number.

Note that parseFloat() is a global function, which means that it can be called without an object. It is often used to parse user input, such as form data or query parameters, which are typically received as strings.