JS in the! =, = =,! = = =, the usage and the difference.
1234567891011121314151617181920 |
var num = 1; var str = ' 1 '; var test = 1; test = = num //true same type same value test = = = num //true same type same value test!== num //false test is the same as NUM type, with the same value The non-operation is definitely false num = = str //true converts str to numbers to check for equality. num! = str //false = = Non-op num = = = str //false type, direct return false num!== str //true& The difference between nbsp; num and STR means that their unequal non-operation is naturally true. |
= = and! = Compare if the type is different, the first test conversion type, then the value comparison, the final return value comparison results.
The values of = = = and!== are compared only under the same type. -----------------------------------First, = = equality equivalent, = = = Identity identity.
= =, when the value types on both sides are different, type conversion is performed before comparison.
= =, do not do type conversion, different types of certain range.
The following are respectively explained:
First say = = =, this is relatively simple. The following rules are used to determine if two values are equal to = = =:
1. If the type is different, [unequal]
2, if two are numeric, and is 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 ())
3. If two are strings, the characters are the same for each position, then [equal]; otherwise [unequal].
4, if the two values are true, or both are false, then [equal].
5. If all two values refer to the same object or function, then [equal]; otherwise [unequal].
6, if two values are null, or both are undefined, then [equal].
Say = =, according to the following rules:
1, if two value types are the same, do = = = comparison.
2. If two value types are different, they may be equal. Type conversions are then compared according to the following rules:
A, if one is null and one is undefined, then [equal].
B, if one is a string, one is a numeric value, the string is converted to a numeric value and then compared.
C, if any value is true, convert it to 1 and then compare it to 0 if either value is false.
D, if one is an object and the other is a numeric or string, convert the object to the value of the underlying type and then compare it. 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)
E, any other combination, are [unequal].
Example:
"1" = = True
Type, true will first be converted to a value of 1, now become "1" = = 1, and then the "1" into 1, compared to 1 = 1, equal.
= Assignment operator
= = equals
= = = strictly equals used for rigorous comparative judgment.
Cases:
var a = 3;
var B = "3";
A==b returns True
A===b return False
JS in the! =, = =,! = = =, the usage and the difference.