5.5 The Strict Equality and Inequality Operators

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 5.  Operators

We just saw that the standard equality operator (= =) can test whether two expressions have the same value. The strict equality operator, written as three consecutive equals signs (= = =), tests whether two expressions are of the same datatype before testing whether they have the same value.

For example, the following standard equality test yields true because the interpreter converts the string "13" to the number 13 before comparing the values:

trace("13" =  = 13);  // Displays: true

In contrast, the following strict equality test yields false because the operands are not of the same datatype:

trace("13" =  =  = 13);  // Displays: false

A strict equality test takes the general form:

operand1 =  =  = operand2

where operand1 and operand2 can be any valid expression. The strict equality operator determines the equality of its two operands as follows:

  • If operand1 and operand2 are of different datatypes, they are not equal.

  • If operand1 and operand2 are both undefined, they are equal.

  • If operand1 and operand2 are both null, they are equal.

  • If operand1 and/or operand2 is the special numeric value NaN, they are not equal.

  • If operand1 and operand2 are both numbers and have the same numeric value, they are equal.

  • If operand1 and operand2 are both strings and have the same characters in the same order, they are equal.

  • If operand1 and operand2 are both true or both false, they are equal.

  • If operand1 and operand2 store a reference to the same object, they are equal.

  • Under any other condition, operand1 and operand2 are not equal.

The primary purpose of strict equality is to prevent the interpreter from performing datatype conversions when evaluating the equality of its two operands. For example, the following condition checks both whether numLives is a number and whether it is equal to 0:

if (numLives =  =  = 0) {   trace("Game over"); }

which is more succinct than the alternative:

if ((typeof numLives =  = "number") && (numLives =  = 0)) {   trace("Game over"); }

A strict inequality test takes the form:

operand1 !=  = operand2

The strict inequality operator (!= =) returns the Boolean opposite of the strict equality operator.

The strict equality operator, added in Flash MX, is created using three equals signs in a row (= = =). It should not be confused with either the standard equality operator (= =) or the assignment operator (=). Similarly, strict inequality, also added in Flash MX, is written as an exclamation point followed by two equals signs (!= =), while the standard inequality operator is written as an exclamation point followed by one equals sign (!=).

     



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net