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