The comparison in javascript

Source: Internet
Author: User
Code 1: [] []; false [] []; false {}{}; SyntaxError: Unexpectedtoken {}}; SyntaxError: Unexpectedtoken Code 2: varn0123; varn code 1: [] = []; // false [] = []; // false {}={}; // SyntaxError: unexpected token =={}=={}; // SyntaxError: Unexpected token === Code 2: var n0 = 123; var n1 = new Number (123 ); var n2 = new Number (123); var n3 = Number (123); var n4 = Number (123); n0 = n1; // true n0 = n3; // true n0 = N1; // false n0 = n3; // true n1 = n2; // false n1 = n2; // false n1 = n3; // false n1 = n3; // true n1 = n3; // false n3 = n4; // true n3 = n4; // Why is this result true? We need to carefully read the specifications: http://ecmascript.cn/#203 11.9.3 equal abstraction comparison algorithm comparison calculation x = y, where x and y are values, generate true or false. If Type (x) is the same as Type (y), if Type (x) is Undefined, true is returned. If Type (x) is Null, true is returned. If Type (x) is Number, if x is NaN, false is returned. If y is NaN, false is returned. Returns true if x and y are equal. If x is + 0 and y is −0, true is returned. If x is −0 and y is + 0, true is returned. Returns false. If Type (x) is String, true is returned when x and y are character sequences of the same length (the same characters are in the same position. Otherwise, false is returned. If Type (x) is Boolean, true is returned if x and y are both true or false. Otherwise, false is returned. Returns true if x and y reference the same object. Otherwise, false is returned. If x is null and y is undefined, true is returned. Returns true if x is undefined and y is null. If Type (x) is Number and Type (y) is String, comparison x = ToNumber (y) is returned. If Type (x) is String and Type (y) is Number, the result of comparing ToNumber (x) = y is returned. If Type (x) is Boolean, the result of comparing ToNumber (x) = y is returned. If Type (y) is Boolean, the result of comparing x = ToNumber (y) is returned. If Type (x) is String or Number and Type (y) is Object, the result of comparing x = ToPrimitive (y) is returned. If Type (x) is Object and Type (y) is String or Number, the result of comparing ToPrimitive (x) = y is returned. Returns false. Note: According to the above definition: String comparison can be enforced in this way: "" + a = "" + B. The value comparison can be enforced in this way: + a = + B. The Boolean value comparison can be enforced in this way :! A =! B. Note: The equivalence comparison operation is always the same as the following:! = B is equivalent! (A = B ). A = B is equivalent to B = A, except for the execution sequence of A and B. Note: equal operators are not always passed. For example, two different String objects all represent the same String value. The = operator considers that each String object is equal to the String value, but the two String objects are not equal to each other. For example, new String ("a") = "a" and "a" = new String ("a") are both true. New String ("a") = new String ("a") is false. The string comparison method is to simply check whether the character encoding unit sequence is the same. There will be no more complex semantic-based characters or equal definitions of strings, and the collating order defined in the Unicode specification. Therefore, the Unicode Standard considers that Equal String values may be detected as unequal. In fact, this algorithm considers that two strings are already normalized. 11.9.6 is strictly equal to the comparison algorithms x = y, and x and y are values. true or false must be generated. The comparison process is as follows: if the result of Type (x) and Type (y) is inconsistent, false is returned. Otherwise, if the result of Type (x) is Undefined, true is returned) if the result is Null, true is returned. If the result of Type (x) is Number, if x is NaN, false is returned. If y is NaN, false is returned. If x and y are the same Number, returns true if x is + 0, y is-0, returns true if x is-0, y is + 0, returns true if Type (x) is String, true is returned if x and y are character sequences of identical characters (the same length and the same position for the same characters). Otherwise, false is returned if Type (x) is returned as Boolean, if both x and y are true or false, true is returned. Otherwise, false is returned. Y references to the same Object and returns true. Otherwise, false is returned. Note: This algorithm differs from SameValue in dealing with signed zero and NaN values.

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.