Determining whether two variables are equal is a very important operation in programming. When processing the original value, this operation is quite simple, but the task is a little complicated when it involves objects.
Ecmascript provides two sets of equality operators: equal signs and non-equal signs are used to process original values, and full and non-full signs are used to process objects.
Equal sign and non-equal sign
In ecmascript, the equal sign is expressed by the equal sign (=). if and only when the two arithmetic numbers are equal, true is returned. The non-equal sign is added by the exclamation point (! =) Indicates that it returns true if and only if the number of two operations is not equal. To determine whether two arithmetic operations are equal, both operators perform type conversion.
The rules for executing type conversion are as follows:
- If a number is a Boolean value, convert it to a numeric value before checking equality. Convert false to 0, and true to 1.
- If one operation is a string and the other is a number, convert the string to a number before checking for equality.
- If one operation is an object and the other is a string, try to convert the object to a string before checking equality.
- If one operation is an object and the other is a number, convert the object to a number before checking for equality.
During comparison, this operator also complies with the following rules:
- The null and undefined values are equal.
- When equality is checked, null and undefined cannot be converted into other values.
- If the number of operations is Nan, the equal sign returns false, and the non-equal sign returns true.
- If both arithmetic operations are objects, the referenced values are compared. If the number of two operations points to the same object, the equal sign returns true; otherwise, the number of two operations varies.
Important: Even if both numbers are Nan, the equal sign still returns false, because according to the rule, Nan is not equal to Nan.
The following table lists some special cases and their results:
Expression |
Value |
Null = undefined |
True |
"Nan" = Nan |
False |
5 = Nan |
False |
Nan = Nan |
False |
Nan! = Nan |
True |
False = 0 |
True |
True = 1 |
True |
True = 2 |
False |
Undefined = 0 |
False |
Null = 0 |
False |
"5" = 5 |
True |
Full and non-full equal signs
Similar operators of equal signs and non-equal signs are full and non-full. The two operators are the same as equal signs and non-equal signs, but they do not perform type conversion before checking equality.
The full equal sign is represented by three equal signs (=). True is returned only when the number of type conversion operations is equal.
For example:
VaR snum = "66"; var inum = 66; alert (snum = inum); // output "true" alert (snum = inum ); // output "false"
In this Code, the first alert compares the string "66" and number 66 with the equal sign, and outputs "true ". As mentioned above, this is because the string "66" will be converted to a number 66 before being compared with another number 66. The second alert compares the string and number with the equal sign without type conversion. Of course, the string is not equal to the number, so the output is "false ".
The non-equal sign is added by the exclamation point and two equal signs (! =) Indicates that true is returned only when the number of type conversion operations is not equal.
For example:
VaR snum = "66"; var inum = 66; alert (snum! = Inum); // output "false" alert (snum! = Inum); // output "true"
Here, the first alert uses a non-equal sign to convert the string "66" to the number 66, so that it is equal to the number 66 of the second operation. Therefore, the calculation result is "false" because the two computations are equal. The second alert uses a non-full equal sign. Is the operation "snum" different from "inum? The answer to this question is: Yes (true), because snum is a string, and inum is a number, they are certainly different.