JavaScript relational (comparison) operators

Source: Internet
Author: User
Tags comparison table first string

Operators used for comparison are called as relational operators: less Than (<), greater than (>), less than or equal (<=), greater than or equal (>=), equal (= =), unequal (! =), congruent (identity) (= = =), not congruent (not identical) (!==)

As with other operators, you should follow the rules when the relational operators manipulate non-numeric values:
1. Two operands are numeric, then numerical comparison;
2. The two operands are all strings, then the character encoding values corresponding to two strings are compared;
3. The two operand has one value, then the other is converted to a numeric value, and then the numerical comparison is performed;
4. The two operands have one object, then the ValueOf () method or the ToString () method are called before the results are compared;

    varbox = 3 > 2;//true    varbox = 3 > 22;//false    varbox = ' 3 ' > 22;//false    varbox = ' 3 ' > ' 22 ';//true compares the first character of a numeric string, where the first number of the first string is 3 greater than the first number 2 of the second string, so true    varbox = ' A ' > ' B ';//false a=97,b=98    varbox = ' A ' > ' B ';//true B=66,a is a    varbox = 1 > {};//False if there is toString () or valueOf () returns the value of 1 > return number    varbox = 1>{toString:function() {return2; }}//This compares the return value of the 1> object to 2, the result is false

Second, on equal and unequal comparisons, if the operand is a non-numeric value, follow the rules:
1. An operand is a Boolean value, then it is converted to a numeric value before comparison, and false turns into 0,true to 1;
2. An operand is a string, then the comparison is made before comparing it to a number;
3. An operand is an object, then the ValueOf () or toString () method is called before the comparison with the return value;
4. If no conversion is required, null and undefined are equal;
5. An operand is nan, then = = Returns false,!= True, and Nan is not equal to itself;
6. The two operands are objects, and if they are the same object, return True if they all point to the same object, or false otherwise.
7. Returns true for equal and all-unequal judgments, such as values and types, and returns false otherwise.

   varbox = 2 = = 2;//true    varbox = ' 2 ' = = 2;//true, ' 2 ' will turn into a value of 2    varbox =false= = 0;//True,false turns into a value of 0 .    varbox = ' a ' = = = ' a ';//false, the converted encoding is not the same    varbox = 2 = = {};//false, executing toString () or valueOf () will change    varbox = 2 = = NaN;//false, as long as there is NaN, is false    varbox = {} = = {};//false, compared to their address, each new created object has a different reference address    varAge = {}; varHeight =Age ; varbox = age = = height;//true, the reference address is the same, so equal    varbox = ' 2 ' = = = 2//false, both values and types must be equal    varbox = 2!== 2//false, both values and types are equal

Third, special value comparison table

JavaScript relational (comparison) operators

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.