Operators in JavaScript = = and = = Introduction _javascript Tips

Source: Internet
Author: User

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

The judgment rule of = = = operator

1. Returns False if the two-value type is inconsistent.
2. Returns true if the two values are of the same type and the values are the same. NaN is a special case, Nan===nan returns false.
3. If all two values are of type object, then, as in Java, unless the two references are consistent (reference point to the same object address), the corresponding action returns false even if the contents of object are identical and the values are considered inconsistent. For example, create two identical arrays, and then return the results to false-after they are in the same content, although they are identical in content, but still belong to two different objects.
4.0===-0 returns True.

The judgment rule of = = operator

The = = operator converts a value to a type and then compares it, and its type conversion follows the following principles: precedence is converted to number, and the date object is compared after it is converted to a string. The specific judgment rules are as follows:

1. If two value types are consistent, perform the = = operation and return.
2.null==undefined is true.
3.true will be converted to 1, and false will be compared after conversion to 0.
4. If one of the values is an object, it is converted to number and then compared, except for the Date object.
5. If one of the values is a Date object, it is converted to a string and then compared.

Experiment

Copy Code code 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 = A;
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

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.