Last Updated : Jan 1, 2023
If the string does not contain a numerical value or If the first character of the string is not a Number then it returns NaN.
It actually returns a floating-point number parsed up to that point where it encounters a character that is not a Number.
Here we defined variable ‘v1’ with initialized string value ‘345.789’ and now we have to convert this as float value so we passing that variable ‘v1’ to parseFloat() method.
This method helps us for does the conversion then returns result stored on variable ‘res’. Then we printed result on console by console.log() method with some string and result variable ‘res’.
<!DOCTYPE html> <html> <head> <title>PARSE FLOAT</title> </head> <body> <script> var v1="345.789"; res=parseFloat(v1); console.log("PARSE FLOAT RESULT "+res); </script> </body> </html>
- <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
- The<html> tag is used to indicate the beginning of HTML document.
- As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
- Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
- <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
- In script we defined variable ‘v1’ with keyword ‘var’ there we initialized string value ‘345.789’. Next we need to pass variable ‘v1’ to parseFloat() method then returned result stored on variable ‘res’.
- In variable ‘res’ we have floating point number because we passed string to parseFloat() method. At last we printed using console.log() converted result on console with string ‘PARSE FLOAT RESULT’.
- In script we know that for print on console we need to use console.log() method.
- Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.
When we executes program on browser we can’t see the result because we printed on console panel so we have to use shortcut ‘ctrl+shift+j’ then console panel will open at right hand side with results of ‘PARSE FLOAT RESULT 345.789’.
If your need to define variable ‘v1’ with some other values then you can modify it in this concept accordingly and confirm this whether we specified string contain numbers otherwise we get error.