home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

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











??????????????@Mail.ru