An operator is something, usually a sign like + or -, that is used to manipulate different entities. The entities manipulated are called operands. If you have experience with programming, SQL or even simple computer languages like spreadsheet formulae, you should be familiar with a lot of them already. In the last chapter, we have already introduced the assignment operator. In this chapter, we will go into detail about the most useful operators. (Note: dot and array access operators are also useful, but we'll save that till later when we learn about properties and methods.)

An arithmetic operator is an operator that does maths for you. The four basic arithmetic operators are as follows:

+ does addition.

- does subtraction.

* does multiplication.

/ does division.

Now, you may be tempted to write such a code:

Code

Result

1 + 1;

Error!

However, this will give you an error. This is because 1 + 1 is only an expression with a value of 2; it is not a statement. (Recall: statements tell the computer to do something.) Therefore, we should write this instead:

Code

Result

trace(1 + 1);

2

Again, just take the 'trace' thing for granted for now. Enter this into the ActionScript of the first frame and preview your Flash file. '2' should appear on the output box.

In addition to the above, a less frequently used operator, %, can calculate the remainder of a division operation. 10 % 3, for example, returns 1. Other operations (such as exponents, square roots, etc.) cannot be evaluated using operators and we'll learn them later.

One peculiarity of Flash is that + acts as both the addition operator and the text concatenation operator. The text concatenation operator turns two strings into one. For example, by concatenating "This is" and " Sparta" (note the space before Sparta), we'll get "This is Sparta". If we concatenate "This is" and "Sparta", however, we'll get "This isSparta", which is undesirable.

If the entites on either side of the + sign are both numbers, the + will act as the addition operator. Otherwise, the + will act as a text concatenation operator. Here's an example:

Note that the last one is false because the equality operator is case-sensitive.

When we're working with composite data types, we need to be more careful. The equality operator will only return true if they are referring to the same entity. Even if we have two distinct identical entities, the equality operator will return false. In this example, let's imagine there are two dogs. One is called Spot, and it has another name, Bandian (which is Chinese for 'spot'). The other is called 'Lucky' and they are twins, and are physiologically the same in every way.

Code

Result

trace(spot == bandian);
trace(spot == lucky);

true
false

Sometimes, we compare two entities of different data types, such as 23 and "23". In this case, the equality operator will still return true by automatically converting "23" to 23 for you. If you don't want this automatic conversion, use the strict equality operator (===).

The first line is a comment; we'll talk about that at the end of the chapter. You don't have to understand the third line, either; just assume that it changes the species of nicestDog to 'pekingese'. Now, do you think changing nicestDog's species to 'pekingese' will genetically alter Spot, a chihuahua, into a pekingese? The answer is yes. This is because nicestDog only contains a link to spot.

There are two types of compound assignment operators. We will only talk about the type that combines assignment and arithmetic operators. If you're interested, you can check the official Flash documentation to learn about the other type.

Compound assignment operators always work with numbers. There are five of them, each corresponding to one arithmetic operator:

+=, the addition and reassignment operator

-=, the subtraction and reassignment operator

*=, the multiplication and reassignment operator

/=, the division and reassignment operator

%=, the modulus and reassignment operator

What these operators do is to perform an operation on the left operand and the right operand, then assign the value to the left operand. In other words,

a += b;

is equivalent to

a = a + b;

Let's better = understand this concept with an example.

Code

Result

var number_1:Number = 5;
number_1 *= 2;
trace(number_1);

10

In this example, Flash multiplied number_1 with 5, then assigned the value to number_1.

So far, we've seen that arithmetic operators, logical operators, comparison operators and and the text concatenation operator all return a value. Since assignment operators can be statements per se, does that mean they do not return a value?

In fact, assignment operators do return values. The value returned by an assignment operator is the value assigned to the left operand. For example, a = 2 returns the value 2, while a /= 2 returns half the original value of a.

As you can see, incremental and decremental operators when the are statements per se. However, we sometimes want to place the incremental operator into another statement. With the postfix increment/decrement operators, everything else will be evaluated before going back to the increment/decrement. With the prefix increment/decrement operators, the increment/decrement will be evaluated before everything else.

When one operator is always evaluated before the other, we call it operator precedence.

Here's a quick lowdown on operator precedence:

Operators that only involve one operand, such as NOT (!), are the first to be evaluated. (These are called unary operators if you want to know.)

Next on the list are *, / and %.

Then we have + and -

Then come the comparison operators.

Then the logical operators AND and OR.

The last ones are the assignment operators.

If we want a certain operation to have higher precedence, we should use the grouping operator, which is a pair of round brackets: (). We've already met that before, when we learnt about assignment operators. Let's take another look at that example:

This is because var someNumber:Number = 3 + a is performed first. var someNumber:Number = 3 + a returns a value of 6. You can't assign a value to a constant, or the world would be a very chaotic place! That's why Flash will return an error.

Consider the expression someNumber = 3 * 2 + 5 * 6. 3 * 2 and 5 * 6 have the same precedence, so which one should be performed first? Operator associativity answers this question. In this case, since the associativity of + is 'right to left', 5 * 6 is evaluated first, then 3 * 2, and finally 6 + 30.

The associativities of +, - and assignment operators are all right to left, while those *, /, %, comparison operators and logical operators are left to right.

Let's run a script with lots of operators to see the effects of operator precedence and associativity.

The second trace is much more complicated. The operator with the lowest precedence here is &&, whose precedence is even lower than the comparison operators. The associativity of && is left to right, so the computer needs to evaluate the code on the left of && (1 + 3 / 5 != 2) first. 1 + 3 / 5 = 1.6, which is not equal to 2. Therefore, the left side returns the value true. Now to the right side. In 4 - 3 >= 1, 4 - 3 = 1, so 1 >= 1 returns true. Since both sides of the && are true, the trace statement returns true.

Now that we've learnt all the difficult operators, let's end the chapter with a very simple concept.

A comment is some text that is added to the script to give information. They will be ignored by the compiler. The following example shows two types of comment:

Code

Result

/*This is just an example.
Don't actually copy-paste this into a Flash file.
There will be no visible results. */var a = 6;
var someNumber:Number = 3 + (a += 1); //Assign the sum of 3 and a's new value to someNumber

The first comment, enclosed by /* and */, allows for multiple lines. /* marks the beginning of the comment and */ marks the end of it. This type of comment can also be added in the middle of a line. They are widely used for elements such as to-do lists and copyright information.

The second comment starts with // and is added to the end of the line. It cannot extend over one line. This type of comment is usually used to explain code.

Sometimes, we may want to 'hide' some code for testing or debugging. This process, called commenting out, is also achieved with comments.