Table of Contents

5.5 The Strict Equality and Inequality 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:

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 (!=).


Table of Contents