Relational operator <,<=,>,>= is used to compare strings

Source: Internet
Author: User
Tags first string

Using a relational operator on two strings, many people think that less than means "in alphabetical order", greater than "in alphabetical order", but that is not the case. For a string, the code for each character in the first string is compared to the code for the character at the corresponding position in the second string. When this comparison operation is completed, a Boolean value is returned. The problem is that the code for uppercase letters is smaller than the lowercase code, which means you might encounter the following scenario:

var bresult = "Brick" < "alpha";

alert (bresult);//output:true;

In this example, the string "Brick" is less than the string "alpha" because the character code of the letter B is 66, and the character code for the letter A is 97. To force the results to be obtained alphabetically, you must convert the two operands to the same case (all uppercase or all lowercase) and then compare the rows. For example:

var bresult = "Brick". toLowerCase () < "Alpha". toLowerCase ();

alert (bresult);//Output:false;

Another tricky situation occurs when comparing numbers in the form of two strings, for example:

var bresult= "All" < "3";

alert (bresult);//output:true;

Because both operations are strings, their character code is compared (the character code for "2" is 50; the character code for "3" is 51) so the output is true;

However, it is interesting to change one of the operators in the above example to a number, for example:

var bresult= "3" <;

alert (bresult);//output:false;

If a string number and a number are properly compared, ECMAScript converts the number of the string to a number and then the size of the comparison.

But what if the string cannot be converted to a number?

var bresult= "a" < 3;

alert (bresult);//output:false;

The letter A cannot be swapped for meaningful numbers, but calling the parseint () method returns Nan, and according to the rules, any relational operation that contains Nan will return false, so this code also outputs false;

var bresult= "a" < 3;

alert (bresult);//output:false;

Typically, a greater than or equal operation must return TRUE if the two values of the less-than operation return FALSE, but this is not the case if a number is Nan.

Relational operator <,<=,>,>= is used to compare strings

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.