JS inside the equals number--

Source: Internet
Author: User

One is the assignment equals sign, two is the contrast equals sign, then three equals number is what uses?

Sometimes you see that you use three equals sign (= = =) When judging whether two objects are equal, what is the difference between it and two equals sign (= =)? In simple terms, when using "= =", if the two types are different, the JS engine converts them to the same type and then compares them, while "= = =" Does not convert the type, so when the two sides are not of the same type, it is certainly not equal. For example:

The code is as follows Copy Code

var a = 0, B = ' 0 ';

Alert ((A = = B) + '--' + (a = = b) "The result is" True–false "at this point.

= = = Judgment Rule

If the types are different, [unequal] if two are numeric and are the same value, then [equal]; Exception) is, if at least one of them is Nan, then [unequal].  (To determine whether a value is Nan, you can only use isNaN () to determine) if two are strings, each position has the same characters, then [equal]; otherwise [unequal].  If all two values are true, or both are false, then [equal].  If all two values refer to the same object or function, then [equal]; otherwise [unequal].  If two values are null, or both are undefined, then [equal]. = = Judgment rule:

If the two value types are the same, the = = = comparison. If two value types are different, they may be equal.  Type conversions are then compared according to the following rules: If one is null and one is undefined, then [equal].  If one is a string, one is a numeric value, the string is converted to a numeric value and then compared.  If either value is true, convert it to 1 again, and if either value is false, convert it to 0 and then compare. If one is an object and the other is a numeric or string, the object is converted to the value of the underlying type and then compared. The object is converted to the underlying type, using its ToString or ValueOf method. JS Core built-in class, will try to valueof before ToString, the exception is that date,date is using the ToString conversion. Non-JS core object, make say (more troublesome, I do not understand) any other combination (array array, etc.), are [unequal].

It is particularly important to note the conversion of true, FALSE, for example:

The code is as follows Copy Code

Alert (true = = 1); //ture alert (true = = 2);//false, true is converted to number, which is 1, of course 1 is not equal to 2//can use!! To convert a data type to Boolean alert (true = =!!). 2)//true,!2 = = = False! (!=2) =!false = True

In addition, in JS, if a variable is used in a logical operation, then the value of the variable is false if there is no initial value or if its value is 0, 0, NULL, "", false, undefined, or Nan. Otherwise, its value is true

So where is the difference between the three equals sign and the two equals sign?

First of all, do a simple introduction, starters has an intuitive understanding

= = Equality equals = = = Identity identity

= = When the value types on both sides are different, type conversion is performed before comparison. = = = does not do type conversion, different types must vary.

= = First conversion type and then comparison, = = = First to determine the type, if not the same type directly to False.

= = = Constant Equals, the comparison of the two sides to absolutely the same

Operation experience the following code will be clear:

The code is as follows Copy Code

Alert (0 = = ""); True alert (0 = = false); True alert ("" = = False); True

Alert (0 = = = ""); False alert (0 = = = False); False alert ("" = = = False); False

JS inside the equals number--

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.