Sometimes you can see that you use three equals (= = =) To determine whether two objects are equal, and what is the difference between a two equals sign (= =)? In simple terms, if you use "= =", if the two sides of different types, JS engine will convert them to the same type and then compare, and "= = =" will not be type conversion, so when both sides are not belong to the same type, certainly not equal. For example:
The code is as follows |
Copy Code |
var a = 0, B = ' 0 '; Alert ((A = = B) + '--' + (a = = = B)] At this point see the result is "true–false." |
= = = Judgment Rule
If the type is different, [not equal]
If all two are numeric and are of the same value, then [equal]; Exception is, if at least one of them is Nan, then [not equal]. (To determine whether a value is Nan and can only be judged by isNaN ())
If all two are strings, the characters in each position are the same, then [equal]; otherwise [unequal].
If all two values are true, or all are false, then [equals].
If all two values refer to the same object or function, [equal]; otherwise [unequal].
If two values are null, or all are undefined, then [equals].
= = Judge rule:
If two value types are the same, do = = = Compare.
If two value types are different, they may be equal. Type conversions are compared according to the following rules:
If one is null and one is undefined, then [equals].
If one is a string, and 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 and compare it to 0 if any value is false.
If one is an object and the other is a numeric value or string, the object is converted to a value of the underlying type and then compared. object to the underlying type, using its ToString or ValueOf method. JS core built-in class, will try to valueof before ToString, the exception is date,date use of ToString conversion. Non-JS core objects, make said (more troublesome, I do not understand)
any other combination, are [unequal].
Special attention should be paid to the conversion of true, FALSE, for example:
The code is as follows |
Copy Code |
Alert (true = = 1); Ture Alert (true = = 2); False, True converts to number, which is 1, of course 1 is not equal to 2 can use!! To convert a data type to a Boolean Alert (true = =!!) 2)//true,!2 = = False! (!=2) =!false = True |
Also in JS, if a variable is used in a logical operation, the variable is false if it has no initial value or its value is 0, 0, NULL, "", false, undefined, or Nan. Otherwise, its value is true
So what's the difference between a three equals number and a two equals number?
First of all, do a simple introduction, starters has an intuitive understanding
= = Equality equivalent = = = Identity identity
= = When the value types are different, you must first type conversion, and then compare.
= = = Do not do type conversion, different types must vary.
= = First convert type and then compare, = = To determine the type, if not the same type directly to False.
= = = = = = = = = = = = = = = =
Run through 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 |
We have "= =" and "= =" difference, do some more in-depth analysis of the introduction
First say = = =, this is relatively simple, the specific rules of comparison are as follows:
1, if the type is different, [not equal]
2, if two are numeric, and is the same value, then [equal]; Exception is, if at least one of them is Nan, then [not equal]. (To determine whether a value is Nan and can only be judged by isNaN ())
3, if two are strings, each position characters are the same, then [equal]; otherwise [unequal].
4, if two values are true, or all false, then [equality].
5, if two values all refer to the same object or function, then [equal]; otherwise [unequal].
6, if two values are null, or are undefined, then [equality].
besides = =, the specific comparison rules are as follows:
1, if two value types are the same, to do = = = comparison, comparison rules ditto
2, if two value types are different, they may be equal. Type conversions and comparisons are made according to the following rules:
A, if one is null, one is undefined, then [equals].
B, if one is a string, and one is a numeric value, the string is converted to a numeric value and then compared.
C, if either value is true, convert it to 1 and compare it to 0 if any value is false.
D, if one is an object, and the other is a numeric value or a string, the object is converted to the underlying type for a comparison. object to the underlying type, using its ToString or ValueOf method. JS core built-in class, will try to valueof before ToString, the exception is date,date use of ToString conversion. Non-JS core objects, make said (more trouble, I do not understand)
E, any other combination (array arrays, etc.), [unequal].