The conversion rules are as follows:
If the type of an operand is Boolean, convert it to numeric type first, convert false to 0, and convert true to 1.
If one of the operands is a string and the other is a number, convert the string to a number for comparison.
If one of the operands is a string and the other is an object, the string will be compared after the tostring method of the object is called.
If one of the operands is numeric and the other is object, convert the object to a numeric value and compare the numbers.
The following provides some special comparisons:
Null and undefined are equal.
Null and undefined are not converted to any other type.
If the result of any operation is Nan, if the comparison is equal, false is returned. If the comparison is not equal, true is returned. Note: Even if both operands are Nan, the returned result is false, that is, Nan is not equal to Nan.
If both operands are objects, compare the referenced values. If the same object is referenced, true is returned. Otherwise, false is returned.
Alert (null = undefined); // true
Alert (undefined = NULL); // true
Alert (true = 1); // true
Alert (false = 0); // true
Alert (true = 2); // false
VaR OBJ = {};
Alert (10 = OBJ); // false
Identical comparison ===and not completely equal! =
The exact same comparison is used to compare whether non-conversion is equal. For example:
VaR A = "100 ";
VaR B = 100;
Alert (A = B); // true
Alert (A = B); // false
= The comparison returns true because "100" is first converted to the number 100, and then compared with the number 100, the result is equal.
=== The comparison returns false because the string "100" is not converted and is not equal to the number 100.
! = Used to compare whether the comparison is not equal without conversion.
Alert (! = B); // false
Alert (! = B); // true
In the first case, false is returned, because the converted values are equal. In the second case, the true result is returned, because if no conversion is performed, one of them is a string, the other is a number, and the other is not equal.