Detailed comparison in javascript (= = and = = =)

Source: Internet
Author: User

Abstract Equality Comparison algorithm

The comparison operation x==y , where x and is the y value, is generated true or false . This comparison is done as follows:

  1. IfType(x)AndType(y)The same, the

    1. If Type(x) Undefined so, return true .
    2. If Type(x) Null so, return true .
    3. If Type(x) Number so, then

      1. If x NaN so, return false .
      2. If y NaN so, return false .
      3. If x the y value is equal to, return true .
      4. If x +0 It is y ?0 , return true .
      5. If x ?0 It is y +0 , return true .
      6. Return false .
    4. If it is Type(x) String , it is returned when it is the x y exact same sequence of characters (equal in length and same character in the same position) true . Otherwise, return false .
    5. If Type(x) Boolean it is, x return when and y for the same or the true same false true . Otherwise, return false .
    6. xreturned when and y to reference the same object true . Otherwise, return false .
  2. If x null It is y undefined , return true .
  3. If x undefined It is y null , return true .
  4. If Type(x) Number and Type(y) is String , returns x == ToNumber(y) The result of the comparison.
  5. If Type(x) String and Type(y) is Number , returns ToNumber(x) == y The result of the comparison.
  6. If Type(x) Boolean So, returns ToNumber(x) == y the result of the comparison.
  7. If Type(y) Boolean So, returns x == ToNumber(y) the result of the comparison.
  8. Type(x) String Number Type(y) Object returns x == ToPrimitive(y) The result of the comparison, if it is or, and is.
  9. Type(x) Object Type(y) String Number returns ToPrimitive(x) == y The result of the comparison, if and OR.
  10. Return false .

Note: According to the above definition of equality:

    • String comparisons can be enforced in this manner: "" + a == "" + b .
    • Numeric comparisons can be enforced in this manner: +a == +b .
    • Boolean comparisons can be enforced in this manner: !a == !b .

Note: The equivalent comparison operation guarantees the following unchanged:

    • A != BEquivalent to !(A==B) .
    • A == BEquivalent to B == A , except for the order of execution of A and B.

Note: the equality operator is not always passed. For example, two different String objects represent the same string value; The == operator considers each object to be String equal to the string value, but the two string objects are not equal. For example:

    • new String("a") == "a"and "a" == new String("a") both true .
    • new String("a") == new String("a")For false .

String comparisons are used in a way that simply detects whether the sequence of character-coded cells is the same. There is no more complex definition of semantic-based character or string equality, and collating order as defined in the Unicode specification. So values that are considered equal in the Unicode standard String may be detected as unequal. In fact, the algorithm thinks that two strings are already normalized.

Strictly equals comparison algorithm

Comparisons x===y , x and y for values, require output true or false . The comparison process is as follows:

  1. If Type(x) Type(y) The result is inconsistent, return false , otherwise
  2. If the Type(x) result is Undefined , returntrue
  3. If the Type(x) result is Null , returntrue
  4. If the Type(x) result is Number , then

    1. If x NaN it is, returnfalse
    2. If y NaN it is, returnfalse
    3. If x the y same number is returned,true
    4. If x +0 , y for, -0 returnstrue
    5. If x -0 , y for, +0 returnstrue
    6. Returnfalse
  5. If the Type(x) result is, if it is identical to the same String x character sequence (the same length and the same character corresponds to the same y position), returns true , otherwise, returnsfalse
  6. If the Type(x) result is Boolean , if it is x y both true or false , it returns true , otherwise, returnsfalse
  7. Returns if x and y references to the same Object object, true otherwise, returnsfalse

Note: This algorithm behaves differently with the Samevalue algorithm in treating signed 0 and NaN.

Detailed comparison in javascript (= = and = = =)

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.