Recipe 1.8.
Checking Equality or Comparing Values
Problem
You want to
check if two values are equal.
Solution
Use the equality (or inequality) or strict equality (or
strict inequality) operator to
compare two values. To check whether a value is a valid number, use
isNaN( ).
Discussion
Equality expressions always return a Boolean value indicating whether the
two values are equal. The equality (and inequality) operators come
in both regular and strict flavors. The regular equality and
inequality operators check whether the two expressions being
compared can be resolved to the same value after converting them to
the same datatype. For example, note that the string "6" and the
number 6 are considered equal because the string "6" is converted
to the number 6 before comparison:
trace(5 == 6); // Displays: false
trace(6 == 6); // Displays: true
trace(6 == "6"); // Displays: true
trace(5 == "6"); // Displays: false
Note that in a project with default settings,
the previous code example won't even compile. That's because it is
compiled with a strict flag, causing the compiler
to be more exact in checking datatypes at compile time. It
complains that it is being asked to compare an int with a
String. To turn off the strict flag, go to the
ActionScript Compiler section of the project's properties, and
uncheck the box next to "Enable compile-time type checking
(-strict)". It is suggested, however, that you leave this option on
for most projects, as it gives you better protection against
inadvertent errors.
The
logical inequality operator
(!=) returns false if two values are equal and
true if they aren't. If necessary, the operands are
converted to the same datatype before the comparison:
trace(5 != 6); // Displays: true
trace(6 != 6); // Displays: false
trace(6 != "6"); // Displays: false
trace(5 != "6"); // Displays: true
Again, this example only compiles if
strict type checking is disabled.
On the other hand, if you have turned off the
strict flag, but you want to perform a strict
comparison in one section of code, you can use the strict
equality and inequality operators, === and !==. These first check
whether the values being compared are of the same datatype before
performing the comparison. Differences in datatypes causes the
strict equality operator to return false and the
strict inequality operator to return true:
trace(6 === 6); // Displays: true
trace(6 === "6"); // Displays: false
trace(6 !== 6); // Displays: false
trace(6 !== "6"); // Displays: true
|
There is a big difference between the assignment
operator (=) and the equality
operator (==). If you use the assignment operator instead
of the equality operator, the variable's value will change rather
than testing its current value.
|
|
Using the wrong operator leads to unexpected
results. In the following example, quantity equals 5 at
first, so you might expect the subsequent if statement to
always evaluate to false, preventing the trace( )
from being executed:
var quantity:int = 5;
// The following code is wrong. It should be if (quantity == 6) instead
if (quantity = 6) {
trace("Rabbits are bunnies.");
}
trace("quantity is " + quantity); // Displays: quantity is 6
However, the example mistakenly uses the
assignment operator
(=) instead of the equality operator (==). That
is, the expression quantity = 6 sets quantity to 6
instead of testing whether quantity is 6. When used in an
if clause, the expression quantity = 6 is treated as
the number 6. Because, any nonzero number used in a test expression
converts to the Boolean TRue, the trace( ) action
is called. Replace the test expression with quantity == 6
instead. Fortunately, the ActionScript 3.0 compiler is smart enough
to recognize this common error and although the code still
compiles, you aren't given a warning.
You can check an item's datatype using the
is operator, as follows:
var quantity:int = 5;
if (quantity is int) {
trace("Yippee. It's an integer.");
}
|
Note that the new ActionScript 3.0 types
int and uint will also test positive as
Numbers.
|
|
However, some numeric values are invalid. The
following example results in quantity being set equal to
NaN (a constant representing
invalid numbers, short for not a number) because the calculation
cannot be performed in a meaningful way:
var quantity:Number = 15 - "rabbits";
Despite its name, NaN is a recognized
value of the Number datatype:
trace(typeof quantity); // Displays: "number"
Therefore, to test if something is not just any
number, but a valid number, try this:
var quantity:Number = 15 - "rabbits";
if (quantity is Number) {
// Nice try, but this won't work
if (quantity != NaN) {
trace("Yippee. It's a number.");
}
}
|
However, you can't simply compare a value to the
constant NaN to check whether it is a valid number. The
ActionScript 3.0 compiler even gives you a warning to this effect.
Instead, you must use the special isNaN( ) function to
perform the test.
|
|
To determine if a number is invalid, use the
special isNaN( ) function, as follows:
var quantity:Number = 15 - "rabbits";
if (isNaN(quantity)) {
trace("Sorry, that is not a valid number.");
}
To test the opposite of a condition (i.e.,
whether a condition is not true) use the logical
NOT operator (!). For example, to check whether a
variable contains a valid number, use !isNAN( ), as
follows:
var quantity:Number = 15 - "rabbits";
if (!isNaN(quantity)) {
// The number is not invalid, so it must be a valid number
trace ("That is a valid number.");
}
Of course, you can perform comparisons using the
well-known comparison operators. For example, you can use the
< and > operators to check if one
value is less than or greater than another value:
trace(5 < 6); // Displays: true
trace(5 > 5); // Displays: false
Similarly, you can use the <= and >= operators to check if one
value is less than or equal to, or greater than or equal to,
another value:
trace(5 <= 6); // Displays: true
trace(5 >= 5); // Displays: true
You should also be aware that ActionScript
compares datatypes
differently. ActionScript datatypes can be categorized either as
primitive
(string, number, and Boolean) or composite (object,
sprite, and array). When you compare primitive
datatypes, ActionScript compares them "by value." In this example,
quantity and total are considered equal because
they both contain the value 6:
var quantity:Number = 6;
var total:Number = 6;
trace (quantity == total); // Displays: true
However, when you compare composite datatypes,
ActionScript compares them "by reference." Comparing items by
reference means that the two items are considered equal only if
both point to exactly the same object, not merely objects with
matching contents. For example, two arrays containing exactly the
same values are not considered equal:
// Create two arrays with the same elements.
var arrayOne:Array = new Array("a", "b", "c");
var arrayTwo:Array = new Array("a", "b", "c");
trace(arrayOne == arrayTwo); // Displays: false
Two composite items are equal only if they both
refer to the identical object, array, or sprite. For example:
// Create a single array
var arrayOne:Array = new Array("a", "b", "c");
// Create another variable that references the same array.
var arrayTwo:Array = arrayOne;
trace(arrayOne == arrayTwo); // Displays: true
See Also
Recipe
5.8
|