This article describes JavaScript's judgment and processing techniques for numbers. Share it for your reference. The specific analysis is as follows:
Javascript polymorphic properties are very cool. You don’t need to remember so many strange variables for a var. However, sometimes you are confused. Why, I obviously add two numbers, but the result is added as strings? This is the bad thing about Javascript var, it is not like php, using a . to indicate that this is a string connection. This is what you need parseFloat to specify that this var is a number. IsNaN needs to be used to determine whether this is a number. When isNaN (a judged var), the result is true, then it is not a number, and the result is false, then it is a number, note here.
The following is a program like this. Enter two numbers and can be added normally. If any of the inputs are not numbers, a prompt pops up. If the inputs are numbers, the result pops up. It is worth noting that in Javascript, 00000.22 will also be considered a number, which is 0.22.
This is how this program is written. At the same time, be careful not only to determine whether num1 or num2 is a number, but also to prevent the user from clicking the cancel button! :
<html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8"/> </head> <body> </body> </html> <script> var num1=window.prompt("Please enter a number"); var num2=window.prompt("Please enter a second number"); if(isNaN(num1)||isNaN(num2)||!num1||!num2) alert("Everyone is not a number!"); else{ var res=parseFloat(num1)+parseFloat(num2); alert("The result of adding two numbers is: "+res); } document.write("The program has been run, let's break up!"); </script>window.prompt can pop up an input box. Although it is rarely used on web pages today and is almost impossible to see. Then, it follows the above process and finally uses document.write to output information overriddenly on the web page. The so-called overridden output information, that is, no matter what content is on the web page, it will be overwritten by the content in document.write. This method is rarely used now.
I hope this article will be helpful to everyone's JavaScript programming.