This article mainly introduces JavaScript's judgment and processing methods on numbers. Examples of common methods and techniques for javascript's judgment on numbers are analyzed, which has some reference value, for more information about how to use JavaScript to determine and process numbers, see the following example. Share it with you for your reference. The specific analysis is as follows:
The property of Javascript polymorphism is very nice. A var does not need to remember so many strange variables. However, sometimes you are confused, why? I am adding two numbers, but is the result added up as a string? This is the bad part of Javascript var. Unlike php, A. is used to indicate that this is a string connection. This is what you need parseFloat to explicitly specify that this var is a number. IsNaN is required to determine whether this is a number. If the result of isNaN is true, it is not a number, and the result is false. If the result is false, it is a number. Pay attention here.
The following is a program that can be added normally by inputting two numbers. If any of the input values is not a number, a prompt is displayed. If the input values are two numbers, the result is displayed, it is worth noting that in Javascript, 00000.22 is also considered as a number, that is, 0.22.
This program is written in this way. Besides judging whether num1 or num2 is a number, it also prevents users from clicking the cancel button! :
Script var num1 = window. prompt ("enter a number"); var num2 = window. prompt ("enter the second number"); if (isNaN (num1) | isNaN (num2) |! Num1 |! Num2) alert ("any one 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 get rid of it! "); Script
Window. the prompt can pop up an input box. Although the web page is rarely used today, it is almost invisible. Then, follow the above process and use document. write is used to output information in the overwriting mode of a webpage. The so-called overwriting mode outputs information. That is to say, whether there is any content on the webpage, it will be output by document. the content in write is overwritten, and this method is rarely used now.
I hope this article will help you design javascript programs.