Core JavaScript 1.5 Reference:Operators:Comparison Operators
From MDC
Contents |
[edit] Summary
| Operators | |
| Implemented in: | JavaScript 1.0
JavaScript 1.3: Added the JavaScript 1.4: Deprecated |
| ECMA Version: | ECMA-262 includes all comparison operators except === and !==.
ECMA-262 Edition 3 adds |
The operands can be numerical or string values. Strings are compared based on standard lexicographical ordering, using Unicode values.
JavaScript has both strict and type-converting equality comparison. For strict equality the objects being compared must have the same type and:
- Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.
- Two numbers are strictly equal when they are numerically equal (have the same number value). NaN is not equal to anything, including NaN. Positive and negative zeros are equal to one another.
- Two Boolean operands are strictly equal if they are both
trueorfalse. - Two objects are strictly equal if they refer to the same Object.
- Null and Undefined types are
==(but not===).
The following table describes the comparison operators:
| Operator | Description | Examples returning true1 |
|---|---|---|
Equal (==) |
If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers; if either operand is a string, the other one is converted to a string |
|
Not equal (!=) |
Returns true if the operands are not equal. If the two operands are not of the same type, JavaScript attempts to convert the operands to an appropriate type for the comparison. |
|
Strict equal (===) |
Returns true if the operands are strictly equal (see above) with no type conversion. |
|
Strict not equal (!==) |
Returns true if the operands are not equal and/or not of the same type. |
|
Greater than (>) |
Returns true if the left operand is greater than the right operand. |
|
Greater than or equal (>=) |
Returns true if the left operand is greater than or equal to the right operand. |
|
Less than (<) |
Returns true if the left operand is less than the right operand. |
|
Less than or equal to (<=) |
Returns true if the left operand is less than or equal to the right operand. |
|
1 These examples assume that var1 has been assigned the value 3 and var2 has been assigned the value 4.
[edit] Using the Equality Operators
The standard equality operators (== and !=) compare two operands without regard to their type. The strict equality operators (=== and !==) perform equality comparisons on operands of the same type. Use strict equality operators if the operands must be of a specific type as well as value or if the exact type of the operands is important. Otherwise, use the standard equality operators, which allow you to compare the identity of two operands even if they are not of the same type.
When type conversion is needed, JavaScript converts String, Number, Boolean, or Object operands as follows.
- When comparing a number and a string, the string is converted to a number value. JavaScript attempts to convert the string numeric literal to a
Numbertype value. First, a mathematical value is derived from the string numeric literal. Next, this value is rounded to nearestNumbertype value. - If one of the operands is
Boolean, the Boolean operand is converted to 1 if it istrueand +0 if it isfalse. - If an object is compared with a number or string, JavaScript attempts to return the default value for the object. Operators attempt to convert the object to a primitive value, a
StringorNumbervalue, using thevalueOfandtoStringmethods of the objects. If this attempt to convert the object fails, a runtime error is generated.
You cannot use the standard equality operator (==) to compare instances of JSObject. Use the JSObject.equals method for such comparisons.
[edit] Backward Compatibility
The behavior of the standard equality operators (== and !=) depends on the JavaScript version.
[edit] JavaScript 1.3 and earlier versions
You can use either the standard equality operator (==) or JSObject.equals to compare instances of JSObject.
[edit] JavaScript 1.2
The standard equality operators (== and !=) do not perform a type conversion before the comparison is made. The strict equality operators (=== and !==) are unavailable.
[edit] JavaScript 1.1 and earlier versions
The standard equality operators (== and !=) perform a type conversion before the comparison is made. The strict equality operators (=== and !==) are unavailable.