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