The difference between the logical operator "= =" and "= = =" in JavaScript

Source: Internet
Author: User

Summary

In JavaScript, the logical operator "= = =" Checks the data type of the operand first, and returns false for different data types. When the "= =" comparison is performed on different types of operands, the type conversion is then compared.

Description

Two logical operators that are judged in javascript:

    1. = = (equal)
    2. = = = (strictly equal/congruent)

Because JavaScript is a weakly typed scripting language (weakly typed), the type of two operands used for comparison allows for inconsistencies. The biggest difference between the two logical operators is the tolerance to the operand type .

That is, if the data type of the two operands is inconsistent

    • "= =" attempts to convert the data type of the operand before comparing it.
    • "= = =" will return false directly.
Console.log (typeofnulltypeof//object undefinedconsole.log (null  null//true False

It can be seen that the results of both comparisons are different because Null is different from the undefined type.

Another example:

if (foo) {    dosomething;} // the case of Foo as false: // false // 0 // "(empty string) // NULL // undefined // NaN
// and except Nan, any combination using "= =" As the result of the comparison is true, using "= = =" To compare the result is false, such as if (' = = 0) is true.
Nan is not equal to any other value, that is, if and only if A=nan, a! = A is true.

For more detailed rules on "= =" and "= = =", refer to my Evernote.

The content is primarily referenced in the JavaScript authoritative guide.

The difference between the logical operator "= =" and "= = =" in JavaScript

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.