The difference between = = and = = in Javascript

Source: Internet
Author: User

"= = =" is called the strict operator, and "= =" is called the equality operator.

The operation rules for strict operators are as follows,

(1) Different types of values

If the two values are of different types, return false directly.

(2) Primitive type values of the same class

When a value of the same type (numeric, String, Boolean) is compared, the same value returns True, and the value returns FALSE.

(3) Composite type values of the same class

When comparing data of two composite types (objects, arrays, functions), they do not compare their values for equality, but rather whether they point to the same object.

(4) Undefined and null

Undefined and null are strictly equal to themselves.

NULL NULL  // trueundefined = = = undefined  //true

The equality operator is exactly the same as the strict equality operator when comparing data of the same type.

When comparing different types of data, the equality operator first converts the data into type conversions and then compares them with strict equality operators. The type conversion rules are as follows:

(1) Value of the original type

Data from the original type is converted to a numeric type and then compared.

Both the string and the Boolean value are converted to numeric values.

(2) Comparing objects to primitive type values

objects, which are generalized objects, including numeric values and functions, are converted to the values of the original type when compared to the values of the original type, and then compared.

(3) Undefined and null

Undefined and null compare to other types of values, the result is false, and they compare with each other when the result is true.

(4) Disadvantages of the equality operator

The type conversions that are hidden by the equality operator introduce some counterintuitive results.

' = = ' 0 '//false0 = = "//true0 = = ' 0 '//truefalse= = ' false '//falsefalse= = ' 0 '//truefalse= = undefined//falsefalse==NULL       //falseNULL= = undefined//true' \t\r\n ' = = 0//true

That's why it is recommended that you try not to use the equality operator .

As to whether using the equality operator would have unintended consequences for subsequent code, the answer might be.

 var  a = undefined;  if  (! a) {Console.log ( "1"); // 1   " var  a = undefined;  if  (A = = null   "1"); // 1  }  var  a = undefined;  if  (A = = null   "1"); //  no output } 

This means that when a is undefined, the value of the output changes, and in programming the object becomes undefined is too common.

Original link http://www.cnblogs.com/DanBrown/p/8960989.html

The difference between = = 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.