"= =" and "= = =" in JavaScript

Source: Internet
Author: User

for JavaScript "= =" and "= = =" Difference I think a lot of people should be very clear, examples:

1==true//true

1===true//false

We usually judge two data, and the "= =" in JavaScript takes an implicit cast, while "= = =" means two data types are equal and values are equal. Therefore, it is recommended to use "= = =" When judging mixed-type data.

So what is the implicit conversion mechanism in JavaScript? In addition to "null", "undefined", for data of non-date type, JavaScript will first attempt to use the method of valueof () to determine equality before using the ToString () method, and the date type is the opposite Let's look at the following example:

null==undefined//true

null===undefined//false

The "null==undefined" is a very special one, in fact the result is true; however, they are not implicitly cast, including their comparison with other types of data and do not implicitly cast.

It is worth mentioning that NULL is the case at the time of operation, let's take a look at an example:

Alert (4+null)

This code does not error, the result of the operation is 4, because NULL is implicitly converted to 0 when the operation. So beware of the implicit coercion type conversion of JavaScript when comparing or computing.

Another fact:

var A=nan; Alert (a===a)//false

Yes, Nan is the only data in JavaScript that is not equal to itself, sometimes we need to determine whether a data equals Nan,javascript also provides a method isNaN (), but this method has a limitation, for example:

Alert (IsNaN (New Object ())); True

Alert (IsNaN ("AAA")); True

This is the limitation of isNaN (), only when we determine that the data type is number, we can use isNaN ();

So how to tell if a data is Nan, we can do this (the wording may be a bit awkward):

if (A!==nan) {}

When we judge whether a variable is equal in JavaScript, we usually write this:

var a= "xxx";

if (a== "xxx") {}

In fact, this writing is a hidden danger, and is caused by human factors, that is, when we write a lot of lines of code when we will inadvertently put if (a== "xxx") written if (a= "xxx") so not only to judge the failure also changed a value, the most troublesome is that the code will not error, was executed so well that it produced unexpected results. Sometimes such errors are difficult to find. Here's a way to avoid this error:

var a= "xxx";

if ("xxx" ==a) {}

End ...

"= =" and "= = =" in JavaScript

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.