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.