Alert object type when JavaScript numeric size comparison

Source: Internet
Author: User
Tags arithmetic numeric

Use JS to get the value of the two-digit input box on the page and compare it to size. The result may be confusing to you, such as code:

The code is as follows Copy Code

var v1 = $ (' #input1 '). Val ();
var v2 = $ (' #input2 '). Val ();
Alert (V1 > V2);

If you enter 2 in the INPUT1 input 10,input2. 10 is definitely greater than 2, so the alert box above should pop true, but it actually gives false. Why? The corresponding number 10 greater than 2 is no doubt, if it is a string comparison, then the result is just the opposite. The type that is worth getting from the page above is string.


Cases

The code is as follows Copy Code

<script>
Alert (' > ' 40 '); False because the 1<4
Alert (> 40.00); True
</script>


In a JS script, you sometimes need to compare the size of two values, and both of these values are numbers,

Such as

The code is as follows Copy Code

function Getbigger (A, b) {

Return a > B? A:B;

}

If you explicitly want to make a numeric comparison, you need to display the conversion to numbers, such as using parseint, parsefloat, and another method is to use the subtraction operator, such as: v1–v2 > 0, when JS in the operation before the type conversion.

Cases

  code is as follows copy code

<script language= "javascript"
Function compare () ... {
if (parseint (Document.form1.a.value) < parseint (Document.form1.b.value)) ... {
Alert ("a <b");
}else ... {
Alert ("A>b");
}

}
</script>
<form name=form1>
<input name=a type=text/>
<input name=b type=text/>
<input type=button onclick= "compare ()" value= "compare"/>
</form

parseint (cString) An integer that is converted from a string cString a non-empty character and stops when the decimal point or other 0-9 characters are encountered, such as " -1234a", "1234.0", which returns 1234; if a 0-9 character except the first symbol is not, Will return nan, such as "-a", "abc", and so on;

In addition, because the addition operator supports numbers and strings at the same time, be careful when using this time. For example A + B, if one is a string type and one is a numeric type, the other is converted to a string type before performing the operation, so it is actually a string linking operation rather than the desired arithmetic operation. In the case of determining whether an arithmetic operation is to be performed, for an unknown type of argument, in the case of insurance, use parseint for type conversion.

Related Article

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.