JavaScript number vs. string comparison size

Source: Internet
Author: User

In summary JS often encounter pure numbers and a variety of strings to compare:

    • A comparison between pure numbers

alert (1<3);//true

    • Numeric string comparisons that convert its first to numbers

Alert ("1" < "3");//true

Alert ("123" < "123");//false

    • Pure string comparison, first turn into ASCII code

Alert ("A" < "B");//true

   Alert ("ABC" < "AAD"),//false, multi-letter comparison, will compare ASCII code in turn

    • Comparison of Chinese characters

   Alert ("Me". charCodeAt ());//25105

Alert ("of". charCodeAt ());//30340

   Alert ("I" < ");//true, kanji comparison, turn ASCII code

    • When a number is compared with a string, and the string is a number. Convert a numeric string to a number

Alert (123< "124"),//true, the following code shows that ASCII code 124 is 49, so it is not converted to ASCII comparison

Alert ("124". charCodeAt ());//49

    • When a number is compared to a string and the string is a non-pure number, converting a non-numeric string to a number is converted to Nan, and false is returned regardless of the size of Nan and the number comparison.

Alert (13> "ABC");//false

JavaScript number vs. string comparison size

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.