This section describes the JavaScript equality and inequality operators. These are operators that compare two values to determine whether they are the same or different and return a boolean value (true or false) depending on the result of the comparison. As we'll see in Chapter 6, they are most commonly used in things like if statements and for loops, to control the flow of program execution.
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 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 identity operator is standardized by ECMAScript v3 and implemented in JavaScript 1.3 and later. With the introduction of the identity operator, JavaScript supports =, ==, and === operators. Be sure you understand the differences between the assignment, equality, and identity operators, and be careful to use the right one when coding! Although it is tempting to call all three operators "equals," it may help to reduce confusion if you read "gets or is assigned" for =, "is equal to" for ==, and "is identical to" for ===.
In JavaScript, numbers, strings, and boolean values are compared by value. In this case, two separate values are involved, and the == and === operators check that these two values are identical. This means that two variables are equal or identical only if they contain the same value. For example, two strings are equal only if they each contain exactly the same characters.
On the other hand, objects, arrays, and functions are compared by reference. This means that two variables are equal only if they refer to the same object. Two separate arrays are never equal or identical, even if they contain equal or identical elements. Two variables that contain references to objects, arrays, or functions are equal only if they refer to the same object, array, or function. If you want to test that two distinct objects contain the same properties or that two distinct arrays contain the same elements, you'll have to check the properties or elements individually for equality or identity. (And, if any of the properties or elements are themselves objects or arrays, you'll have to decide how deep you want the comparison to go.)
The following rules are used to determine whether two values are identical according to the === operator:
If the two values have different types, they are not identical.
If both values are numbers and have the same value, they are identical, unless either or both values are NaN, in which case they are not identical. The NaN value is never identical to any other value, including itself! To check whether a value is NaN, use the global isNaN( ) function.
If both values are strings and contain exactly the same characters in the same positions, they are identical. If the strings differ in length or content, they are not identical. Note that in some cases, the Unicode standard allows more than one way to encode the same string. For efficiency, however, JavaScript string comparison compares strictly on a character-by-character basis, and it assumes that all strings have been converted to a "normalized form" before they are compared. See the "String.localeCompare( )" reference page in the core reference section of this book for another way to compare strings.
If both values are the boolean value true or both are the boolean value false, they are identical.
If both values refer to the same object, array, or function, they are identical. If they refer to different objects (or arrays or functions) they are not identical, even if both objects have identical properties or both arrays have identical elements.
If both values are null or both values are undefined, they are identical.
The following rules are used to determine whether two values are equal according to the == operator:
If the two values have the same type, test them for identity. If the values are identical, they are equal; if they are not identical, they are not equal.
If the two values do not have the same type, they may still be 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.
If one value is an object and the other is a number or string, convert the object to a primitive and try the comparison again. An object is converted to a primitive value by either its toString( ) method or its valueOf( ) method. The built-in classes of core JavaScript attempt valueOf( ) conversion before toString( ) conversion, except for the Date class, which performs toString( ) conversion. Objects that are not part of core JavaScript may convert themselves to primitive values in an implementation-defined way.
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 numbers are now the same, the comparison returns true.
When the equality operator in JavaScript 1.1 attempted to convert a string to a number and failed, it displayed an error message noting that the string could not be converted, instead of converting the string to NaN and returning false as the result of the comparison. This bug has been fixed in JavaScript 1.2.
The == operator always behaves as described previously, and the != operator always behaves as described in the next section, with one exception. In client-side JavaScript in Netscape 4 and later, when embedded in a <script> tag that explicitly specifies JavaScript 1.2 as its language attribute, the equality operator behaves like the identity operator, and the inequality operator behaves like the non-identity operator. To avoid this incompatibility, never use the language="JavaScript1.2" attribute to embed your client-side JavaScript code. See Section 11.6, for a complete list of similar JavaScript 1.2 incompatibilities.
The != and !== operators test for the exact opposite of the == and === operators. The != inequality operator returns false if two values are equal to each other and returns true otherwise. The !== non-identity operator returns false if two values are identical to each other and returns true otherwise. Note that this operator is standardized by ECMAScript v3 and implemented in JavaScript 1.3 and later.
As we'll see, the ! operator computes the Boolean NOT operation. This makes it easy to remember that != stands for "not equal to" and !== stands for "not identical to." See the previous section for details on how equality and identity are defined for different data types.
Copyright © 2003 O'Reilly & Associates. All rights reserved.