javascript-number and string comparison size

Source: Internet
Author: User

JavaScript often compares the size of strings, sometimes confusing, so simply comb the comparison of JavaScript strings:

    1. Number comparison    Console.log (' number comparison: ' + (< 3) ');//false    //2. String numbers and numbers are uniformly converted to numbers for comparison    console.log (' string numbers and numeric comparisons: ' + (< ' 5 ')); False    //3. When strings and numbers are compared to a non-pure number, a non-numeric string is converted to a number when it is turned to Nan, and false    is returned regardless of the size of Nan and the number comparison. Console.log (' string and number comparison: ' + (> ' flyelephant ')); False    Console.log (' string and number comparison: ' + (< ' flyelephant ') ');//false    //4. String number comparison convert to ASCII code comparison    Console.log (' String number comparison: ' + (' one ' < ' 5 ')); Fasle    console.log (' One '. charCodeAt ());//49    console.log (' 5 '. charCodeAt ());//53    //5. String comparison Convert to ASCII code to compare    Console.log (' string comparison: ' + (' Blog Park ' < ' flyelephant '));    Console.log (' Blog Park '. charCodeAt ()); 21338    console.log (' Flyelephant '. charCodeAt ());//70

From the above code we can conclude that the string comparison is more inclined to convert to a number comparison, only two operand is a string when the ASCII code conversion is compared ~

javascript-number and string comparison size

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.