How jquery determines the size of two values

Source: Internet
Author: User
This article mainly share with you how jquery determines the size of two values, mainly in the form of code and we share, hope to help everyone.

$a = ten; $b = 2;if ($a < $b) {echo  "B is greater than a";} Else{echo "A greater than B"};

In JS, VAR defines variables and generates strings.

var a = 10;var B = 2;if (A < b) {alert ("error");} Else{alert ("normal");};

The running result is normal; 10 is less than 2 normal. Obviously, this is not the result we want. Why? Because Two is a string, take the first bit of a 1, take the first bit of B 2; Obviously, 2 is greater than 1. Then return to normal.

Workaround:

One, eval () function

if (eval (a) <eval (b)) {//Logical business} The   eval () function is used to calculate a code string without referencing any particular object.

Second, parseint () function

The IF (parseint (a) <parseint (b)) {//Logical business}   parseint () function is used in converting to int.

Multiplication operation (disguised conversion type) (primary school sports teacher once said: Both sides multiplied by the same number, the equation does not change)

if (a *10 < b * 10) {//Logic business}

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.