The comparison operators (also called relational operators) are used to determine which of two values appears first in a given order. Like the equality and inequality operators, the comparison operators return one of the Boolean values, true or false, indicating whether the relationship described in the comparison is accurate (true) or inaccurate (false).
Comparison operators are intended for comparing strings and numbers; when used with other types, automatic datatype conversion occurs, as discussed later in this chapter. When the two operands of a comparison operator are numbers, the comparison is performed mathematically: 5 < 10 is true, -3 < -6 is false, and so on. When the two operands of a comparison operator are strings, the comparison is performed according to character code points, as described in Chapter 4 under Section 4.6.2.2.
The interpreter attempts to convert any nonstring or nonnumeric data value used in a comparison operation to the string or number type. We'll consider the effect of datatype conversions on comparison operations after we discuss the comparison operators themselves.
The less-than operator takes the general form:
operand1 < operand2
If the operands are numeric, the less-than operator returns the Boolean true if operand1 is mathematically smaller than operand2:
5 < 6 // true 5 < 5 // false; they are equal, but 5 is not less than 5 -3 < -6 // false; -3 is larger than -6 -6 < -3 // true; -6 is smaller than -3
If the operands are strings, the less-than operator returns true if operand1 comes before operand2 in the Unicode character set; otherwise, it returns false:
"a" < "z" // true; lowercase "a" comes before lowercase "z" "A" < "a" // true; uppercase letters come before lowercase "Z" < "a" // true; uppercase letters come before lowercase "hello" < "hi" // true; "e" comes before "i"
For further string comparison details, see Section 4.6.2.2 in Chapter 4.
The greater-than operator takes the general form:
operand1 > operand2
If the operands are numeric, the greater-than operator returns the Boolean true if operand1 is mathematically larger than operand2:
5 > 6 // false 5 > 5 // false; they are equal, but 5 is not greater than 5 -3 > -6 // true; -3 is greater than -6. -6 > -3 // false; -6 is not greater than -3.
If the operands are strings, the greater-than operator returns true if operand1 comes after operand2 in the Unicode character set; otherwise, it returns false:
"a" > "z" // false; lowercase "a" comes before lowercase "z" "A" > "a" // false; uppercase letters don't come after lowercase "Z" > "a" // false; uppercase letters don't come after lowercase "hello" > "hi" // false; "e" comes before "i"
For further string comparison details, see Section 4.6.2.2 in Chapter 4.
The less-than-or-equal-to operator takes the general form:
operand1 <= operand2
If the operands are numeric, the less-than-or-equal-to operator returns the Boolean true if operand1 is mathematically smaller than or equal to operand2:
5 <= 6 // true 5 <= 5 // true; note the difference from 5 < 5 -3 <= -6 // false -6 <= -3 // true
If the operands are strings, this operator returns true if operand1 comes before operand2 in the Unicode character set or if the operands are identical (according to the rules described in Section 4.6.2); otherwise, <= returns false:
"a" <= "z" // true; lowercase "a" comes before lowercase "z" "A" <= "a" // true; although not equal, "A" comes before "a" "Z" <= "a" // true; uppercase letters come before lowercase "hello" <= "hi" // true; "e" comes before "i"
Note that the <= operator is written with the equals sign after the less-than sign. The following is not a valid operator: =<.
The greater-than-or-equal-to operator takes the general form:
operand1 >= operand2
If the operands are numeric, the greater-than-or-equal-to operator returns the Boolean true if operand1 is mathematically larger than or equal to operand2:
5 >= 6 // false 5 >= 5 // true; note the difference from 5 > 5 -3 >= -6 // true -6 >= -3 // false
If the operands are strings, this operator returns true if operand1 comes after operand2 in the Unicode character set or if the operands are identical (according to the rules described in Chapter 4, Section 4.6.2); otherwise, >= returns false:
"a" >= "z" // false; lowercase "a" comes before lowercase "z" "A" >= "a" // false; "A" comes before "a" and they are not equal "Z" >= "a" // false; uppercase letters come before lowercase "hello" >= "hi" // false: "e" comes before "i"
Note that the >= operator is written with the equals sign after the greater-than sign. The following is not a valid operator, although it does make a nice emoticon: =>.
Most of the time, when we're using comparison operators, we're comparing numbers. Hence, type conversions instigated by the comparison operators favor numbers (just like the equality and inequality operators, discussed earlier). Note that this differs from the + operator, which favors conversion to the string datatype. When the two operands of any comparison operator belong to different datatypes, or when neither operand is a string or a number, the interpreter attempts a type conversion according to the following steps:
If both operands are numbers, compare the operands mathematically and return the result. If either number is (or if both numbers are) NaN, the result of the comparison is false, except in the case of the != operator.
If both operands are strings, compare the operands by Unicode code point and return the result. For characters in the Latin alphabet, this is a case-sensitive, alphabetic comparison.
If one operand is a number and the other is a string, convert the string to a number and go back to Step 1.
If either operand is a Boolean, null, or undefined, convert the operand to a number and go back to Step 1.
If either operand is an object, invoke its valueOf ( ) method to convert the object to a primitive value and go back to Step 1. If the valueOf ( ) method fails or does not return a primitive value, return false.
For any other condition, return false.
Note that type conversions performed during a comparison do not alter the original item's stored value or datatype. The results of the temporary conversions are discarded once the expression has been evaluated.
Here is a simple conversion example comparing two Booleans:
false < true // true: 0 is less than 1
Comparison operators always convert composite datatypes to strings or numbers for comparison. In the following example, because both someObj and someOtherObj are members of the Object class, their string value, "[object Object]", is the same:
someObj = new Object(); someOtherObj = new Object(); someObj <= someOtherObj; // true!
In contrast, the expression someObj = = someOtherObj yields false because objects are equality-tested by reference, and they are therefore not converted to primitives during an equality test.
In the next example, even though "A" has the code point 65, converting "A" to a number yields NaN, which means the whole expression yields false. Use the charCodeAt( ) function to check a string's code point:
"A" <= 9999 // false "A".charCodeAt(0) < 9999 // true