4.9.1 Equality and Inequality Operators
The == and === operators check whether two values are the same, using two different definitions of sameness. Both operators accept operands of any type, and both return true if their operands are the same and false if they are different. The === operator is known as the strict equality operator (or sometimes the identity operator), and it checks whether its two operands are “identical” using a strict definition of sameness. The == operator is known as the equality operator; it checks whether its two operands are “equal” using a more relaxed definition of sameness that allows type conversions.
The != and !== operators test for the exact opposite of the == and === operators. The !=inequality operator returns falseif two values are equal to each other according to ==and returns trueotherwise. The !==operator returns falseif two values are strictly equal to each other and returns true otherwise. As you’ll see in §4.10 , the ! operator computes the Boolean NOT operation. This makes it easy to remember that != and !== stand for “not equal to” and “not strictly equal to.”
The strict equality operator === evaluates its operands, and then compares the two values as follows, performing no type conversion:
- If the two values have different types, they are not equal.
- If both values are null or both values are undefined, they are equal.
- If both values are the boolean value true or both are the boolean value false, they are equal.
4.9 Relational Expressions | 71
- If one or both values is NaN, they are not equal. The NaN value is never equal to any other value, including itself! To check whether a value x is NaN, use x !== x. NaN is the only value of x for which this expression will be true.
- If both values are numbers and have the same value, they are equal. If one value is 0 and the other is -0, they are also equal.
- If both values refer to the same object, array, or function, they are equal. If they refer to different objects they are not equal, even if both objects have identical properties.
The equality operator == is like the strict equality operator, but it is less strict. If the values of the two operands are not the same type, it attempts some type conversions and tries the comparison again:
- If the two values have the same type, test them for strict equality as described above. If they are strictly equal, they are equal. If they are not strictly equal, they are not equal.
- If the two values do not have the same type, the ==operator may still consider them equal. Use the following rules and type conversions to check for equality:
- If one value is null and the other is undefined, they are equal.
- If one value is a number and the other is a string, convert the string to a number and try the comparison again, using the converted value.
- If either value is true, convert it to 1 and try the comparison again. If either value is false, convert it to 0 and try the comparison again.
— Any other combinations of values are not equal. As an example of testing for equality, consider the comparison:
"1" == true
This expression evaluates to true, indicating that these very different-looking values are in fact equal. The boolean value true is first converted to the number 1, and the comparison is done again. Next, the string "1"is converted to the number 1. Since both values are now the same, the comparison returns true.欢迎转载,转载请注明来自一手册:http://yishouce.com/book/1/27796.html