Introduction to operators ==and ==in JavaScript, javascript Operators

Source: Internet
Author: User

Introduction to operators ==and ==in JavaScript, javascript Operators

In JavaScript, The = and = operators can be used to determine whether two values are equal. The difference is that if the two types of values to be determined are inconsistent, the = Operator returns false directly, while the = Operator returns false after type conversion. The detailed judgment rules are as follows:

=== Operator Judgment Rules

1. If the two values have different types, false is returned.
2. If the two values are of the same type and value, true is returned. NaN is a special case. NaN = NaN returns false.
3. if both values are of the object type, they are the same as Java, unless the two references are the same (the reference points to the same object address). Otherwise, even if the content in the object is the same, the two values are also considered inconsistent, and the corresponding operation will return false. For example, if you create two arrays with identical content and perform the = operation on them, the returned result is false. Although their content is identical, they still belong to two different objects.
4.0 =-0 returns true.

= Operator Judgment Rules

= The operator converts the value type before comparison. The type conversion principle is as follows: Convert the value to number first for comparison, and convert the Date object to string first for comparison. The specific judgment rules are as follows:

1. If the two values are of the same type, the result is returned after the = operation is executed.
2. null = undefined is true.
3. true is converted to 1 for comparison, and false is converted to 0 for comparison.
4. If one of the values is an object, convert it to number before comparison, except for the Date object.
5. If one of the values is a Date object, convert it to a string before comparison.

Lab

Copy codeThe Code is as follows:
Console. log ("3" === 3); // false
Console. log (NaN = NaN); // false
Var a = {x: 1, y: 2 };
Var B = {x: 1, y: 2 };
Var c =;
Console. log (a = B); // false
Console. log (a = c); // true
Console. log (0 =-0); // true

Console. log ("3" = 3); // true
Console. log (null = undefined); // true
Console. log (true = 1); // true
Console. log (true = 9); // false

Console. log ([9] = 9); // true
Console. log ([9] = "9"); // true

Var d = new Date ();
Var s = d. toString ();
Var n = d. valueOf ();
Console. log (d = s); // true
Console. log (d = n); // false

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.