JS comparison of two number of size instances _javascript tips

Source: Internet
Author: User
Tags comparison eval

Under normal circumstances:

if (2 >)
{
alert ("Incorrect!");
} 

This comparison will not be the desired result: it is equivalent to 2 >1, comparing the first bit of 10 out.

Workaround:

if (eval (2) > eval)
{
alert ("Incorrect!");
} 

The eval () function is used to compute a code string without referencing any particular object.

<script>  
 function  Check ()  
 {  
 var  num1=document.form1.num1.value;  
 var  num2=document.form1.num2.value;  
 if (NUM2>NUM1)  <!-error notation-->
 {  
 alert (' num2  >  num1! ');  
 return  false;  
 }  
 return  true;  
 }  
 </script> 


<script>  
 function  Check ()  
 {  
 var  num1= Document.form1.num1.value;  
 var  num2=document.form1.num2.value;  
 if (parseint (num2) >parseint (NUM1))  <!-correct notation (converted to int)-->
 {  
 alert (' num2  >  num1! ');  
 return  false;  
 }  
 return  true;  
 }  
 </script> 

EG:

110 and 18 are 18 large in the program you're writing, because

These two numbers are strings, and 1 and 1 are equal after 1 and 8, of course 8, so 18 large

You convert to int before comparing

if (parseint (num2) >parseint (NUM1))

The above JS comparison of two numerical size example is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.