The difference between equal (= =) and identity (= = =) in JS

Source: Internet
Author: User

= = When data types are inconsistent on both sides of an expression, they are implicitly converted to the same data type, and then the values are compared.

= = = does not perform type conversions, and compares the data types on both sides, in addition to comparing values.

In addition, the value is null, "", when Undefined,nan, the return is also false. Sometimes it is not necessary to list out a line of code to solve the matter, do not write two lines.

Console.log (Boolean (null)); // false

Console.log (Boolean ("")); // false

Console.log (Boolean (undefined)); // false

Console.log (Boolean (NaN)); // false

Console.log (Boolean (1= =1)); // true

Console.log (Boolean (1= ="1")); // true

Console.log (Boolean (1= = =1)); // true

Console.log (Boolean (1= = ="1")); // false

Besides, the difference between null, Nan, and undefined is also seen from the code that runs below.

varA1;varA2 =true; vara3 = 1; vara4 = "a"; varA5 =NewObject ();varA6 =NULL; varA7 =NaN;varA8 =Undefined;console.log (typeof(a));//"undefined"Console.log (typeof(A1));//"undefined"Console.log (typeof(A2));//"Boolean"Console.log (typeof(A3));//"Number"Console.log (typeof(A4));//"string"Console.log (typeof(A5));//"Object"Console.log (typeof(A6));//"Object"Console.log (typeof(A7));//"Number"Console.log (typeof(A8));//"undefined"Console.log (Boolean (A1= = A6));//trueConsole.log (Boolean (a1 = = A7));//falseConsole.log (Boolean (a6 = = A7));//falseConsole.log (Boolean (a7 = = A7));//false

You can see that undefined values and definitions are not assigned to Undefined,null is a special Object,nan is a special number.

Undefined is equal to null, and Nan is not equal to any value, nor is it equal to itself.

The difference between equal (= =) and identity (= = =) in JS

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.