
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
The Equality and Inequality Operators
|
121
The Equality and Inequality Operators
We use the equality operator (==) to test whether two expressions have the same
value. An equality test takes the general form:
operand1 = = operand2
where operand1 and operand2 can be any valid expression. The equality operator can
compare operands of any type. When
operand1 and operand2 are equal, the expres-
sion returns the Boolean value
true; when they differ, it returns the Boolean value
false. For example:
// Store the value 2 in x.
var x = 2;
// Is x equal to 1?
if (x = = 1) {
// false; nothing is displayed
trace("x is equal to 1);
}
// Is x equal to 2?
if (x = = 2) {
// true; message is displayed
trace("x is equal to 2);
}
The equality operator is created using two equals signs in a row (==). It
determines whether two expressions are equal, and it should not be
confused with the assignment operator (
=), which is a single equals
sign used to assign a new value to a variable. Flash MX also supports
the strict equality operator, created using three equals signs (
===). See
the “The Strict Equality and Inequality Operators” later in this chapter.
Consider this example:
if (x = 5) {
trace("x is equal to 5")
}
This example does not check whether x equals 5. Instead, it sets x equal to 5. It then
evaluates the variable ...