About operators 191
5. Select Control > Test Movie again.
The string
It equals 2 doesn’t appear in the Output panel.
6. Return to the authoring environment and change:
if (myNum == 2) {
to
if (myNum = 2) {
7.
Select Control > Test Movie again.
The string
It equals 2 appears in the Output panel again.
In step 6, you assign the value of
2 to myNum, instead of comparing myNum to 2. In this
case, the
if statement executes regardless of the previous value of myNum, which can cause
unexpected results when you test the Flash document.
For more information on correctly using the assignment operator, see “Using assignment
operators” on page 194.
The strict equality operator (
===) is similar to the equality operator, except it doesn’t perform
type conversion. If two operands are different types, the equality operator returns
false. The
strict inequality operator (
!==) returns the opposite of the strict equality operator.
The following ActionScript demonstrates the key difference between the equality operator
(
==) and the strict equality operator (===):
var num1:Number = 32;
var num2:String = new String("32");
trace(num1 == num2); // true
trace(num1 === num2); // false
First, you define numeric variables: num1, and num2. If you compare the variables using the
equality operator, Flash tries to convert the values to the same data type and then compare the
values to see whether they are equal. When you use the strict equality operator (
===) Flash
doesn’t attempt to do any data type conversion before it compares the values. As a result, Flash
sees the variables as two separate values.
In the following example, you’ll use the greater than or equal to (
>=) operator to compare
values and execute code based on the value a user enters into a text field.