start page | rating of books | rating of authors | reviews | copyrights

Book HomeActionScript: The Definitive GuideSearch this book

5.5. The Comparison Operators

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 work only with strings and numbers. 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 shown in Appendix B, "Latin 1 Character Repertoire and Keycodes". See Section 4.6.2, "Comparing Strings" in Chapter 4, "Primitive Datatypes" for details on string comparisons.

The interpreter will attempt 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.

5.5.1. The Less-Than Operator

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 "alphabetically" before operand2 (see Appendix B, "Latin 1 Character Repertoire and Keycodes"); otherwise, 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" is less than "i"

5.5.2. The Greater-Than Operator

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 "alphabetically" after operand2 (see Appendix B, "Latin 1 Character Repertoire and Keycodes"); otherwise, 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" is less than "i"

5.5.3. The Less-Than-or-Equal-to Operator

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 "alphabetically" before operand2 or if the operands are identical according to the rules described under Section 4.6.2, "Comparing Strings" in Chapter 4, "Primitive Datatypes"; otherwise, it 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" is less than "i"

Note that the <= operator is written with the equal sign after the less-than sign. The following is not a valid operator: =<.

5.5.4. The Greater-Than-or-Equal-to 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 "alphabetically" after operand2 or if the operands are identical according to the rules described under Section 4.6.2, "Comparing Strings" in Chapter 4, "Primitive Datatypes"; otherwise, it 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" is less than "i"

Note that the >= operator is written with the equal sign after the greater-than sign. The following is not a valid operator: =>.

5.5.5. Comparison Operations and Datatype Conversion

Most of the time, when we're using comparison operators we're comparing numbers. Type conversions instigated by the comparison operators, hence, favor numbers. When the two operands of any comparison operator belong to different datatypes, or when neither operand is a string or a number, a type conversion is attempted according to the following steps:

  1. If both operands are numbers, compare the operands mathematically and return the result. If either number is (or both numbers are) NaN, the result of the comparison is false except in the case of the != operator.

  2. If both operands are strings, compare the operands alphabetically using the code points shown in Appendix B, "Latin 1 Character Repertoire and Keycodes" and return the result.

  3. If one operand is a number and the other is a string, convert the string to a number and go back to step 1.

  4. If either operand is a Boolean, null, or undefined, convert the operand to a number and go back to step 1.

  5. 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.

  6. 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 conversion 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 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


Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.