JavaScript Learning notes equality symbol and strict equality symbol _ basic knowledge

Source: Internet
Author: User

Javascript has two ways of determining whether two values are equal.

Equality symbol

The equality symbol consists of two equals: = =
Javascript is a weakly typed language. This means that the equality symbol will cast the type in order to compare two values.

Copy Code code as follows:

"" = = "0"//False
0 = ""//True
0 = "0"//True
False = = "false"//False
false = = "0"//True
false = = undefined//False
false = = NULL//False
NULL = = undefined//True
"\t\r\n" = 0//True

The above code shows the result of a type conversion, so we know that using the equality symbol = = is a bad programming habit. Because of the complex type conversion mechanism in Javascript, the resulting error becomes difficult to trace.
In addition, casts of a type can have a certain effect on performance, for example, when a string is compared to a number and cast to a number.

Strict equality symbol

The strict equality symbol consists of three equals: = = =
It is similar to the operation of the equality symbol, but the strict equality symbol does not do the operation of the coercion type conversion.

Copy Code code as follows:

"" = = = "0"//False
0 = = ""//False
0 = = "0"//False
false = = = "false"//False
false = = = "0"//False
false = = = undefined//False
false = = = NULL//False
NULL = = undefined//False
"\t\r\n" = = 0//False

The code above makes the code clearer, and returns False if the type of two values is different, which can also improve performance.

Compare objects

although = = and = = are called equality symbols, the performance will vary greatly when one of the two values of the comparison is an object.

Copy Code code as follows:

{} === {}; False
New String (' foo ') = = ' Foo '; False
New Number (10) = = 10; False
var foo = {};
Foo = = = = Foo; True

Here, no longer just compare two values for equality, it will determine whether two values refer to the same object instance, and this behavior is more like a pointer in C.

Summarize

It is strongly recommended that only strict equality symbols = = = be used. If we need to do a type conversion, we can do an explicit type conversion before comparing it to the Javascript itself, rather than the complex cast method.

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.