Core JavaScript 1.5 Guide:Operators:Comparison Operators
From MDC
[edit] Comparison Operators
A comparison operator compares its operands and returns a logical value based on whether the comparison is true. The operands can be numerical, string, logical, or object values. Strings are compared based on standard lexicographical ordering, using Unicode values. In most cases, if the two operands are not of the same type, JavaScript attempts to convert the operands to an appropriate type for the comparison. (The sole exceptions to this rule are === and !==, which perform "strict" equality and inequality and which do not attempt to convert the operands to compatible types before checking equality.) This generally results in a numerical comparison being performed. The following table describes the comparison operators, assuming the following code:
var var1 = 3, var2 = 4;
| Operator | Description | Examples returning true |
|---|---|---|
| Equal (==) | Returns true if the operands are equal. | 3 == var1
3 == '3' |
| Not equal (!=) | Returns true if the operands are not equal. | var1 != 4 |
| Strict equal (===) | Returns true if the operands are equal and of the same type. | 3 === var1 |
| Strict not equal (!==) | Returns true if the operands are not equal and/or not of the same type. | var1 !== "3" |
| Greater than (>) | Returns true if the left operand is greater than the right operand. | var2 > var1 |
| Greater than or equal (>=) | Returns true if the left operand is greater than or equal to the right operand. | var2 >= var1 |
| Less than (<) | Returns true if the left operand is less than the right operand. | var1 < var2 |
| Less than or equal (<=) | Returns true if the left operand is less than or equal to the right operand. | var1 <= var2 |
Table 3.3: Comparison operators