1, for String,number and other basic types, = = and = = = There is a difference
1) Comparison of different types, = = Comparison of "converted to the same type of value" to see if "value" is equal, = = = If the type is different, the result is not equal to 2) the same type comparison, direct "value" comparison, the results of the same
2, for Array,object and other advanced types, = = and = = = There is no difference betweenMake a "pointer address" comparison
3, the basic type and the advanced type, = = and = = = There is a difference1) for = =, the advanced conversion to the underlying type, the "value" Comparison 2) because the type is different, = = = The result is False
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:
var a = 0, B = ' 0 '; alert ((A = = B) + '--' + (a = = b))
The result you see is "True–false".
= = = 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]. (Determine if a value is Nan and can only be judged by isNaN ())
- If all two are strings, the characters are the same for each position, 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 of the object, so that (more trouble, I do not understand)
- Any other combinations, are [unequal].
It is particularly important to note the conversion of true, FALSE, for example:
Alert (true = = 1); Turealert (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
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.
The difference between js = = = = = =