About operator precedence and associativity

When you use two or more operators in a statement, some operators take precedence over other operators. Operator precedence and associativity determine the order in which operators are processed. ActionScript has a hierarchy that determines which operators execute before others. There is a table that outlines this hierarchy at the end of this section.

Although it may seem natural to those familiar with arithmetic or basic programming that the compiler processes the multiplication (*) operator before the addition (+) operator, the compiler needs explicit instructions about which operators to process first. Such instructions are collectively referred to as operator precedence.

You can see an example of operator precedence when you work with the multiplication and addition operators:

var mySum:Number;
mySum = 2 + 4 * 3;
trace(mySum); // 14

You see the output of this statement is 14, because multiplication has a higher operator precedence. Therefore, 4 * 3 is evaluated first and the result is added to 2.

You can control what happens by enclosing expressions in parentheses. ActionScript defines a default operator precedence that you can alter using the parentheses (()) operator. When you put parentheses around the addition expression, ActionScript performs the addition first:

var mySum:Number;
mySum = (2 + 4) * 3;
trace(mySum); // 18

Now the output of this statement is 18.

It's also possible for operators to have the same precedence. In this case, the associativity determines the order in which the operators perform. You can either have left-to-right associativity or right-to-left associativity.

Take a look at the multiplication operator again. It has left-to-right associativity, so the following two statements are the same.

var mySum:Number;
var myOtherSum:Number;
mySum = 2 * 4 * 3;
myOtherSum = (2 * 4) * 3;
trace(mySum); // 24
trace(myOtherSum); // 24

You might encounter situations in which two or more operators of the same precedence appear in the same expression. In these cases, the compiler uses the rules of associativity to determine which operator to process first. All of the binary operators, except the assignment operators, are left-associative, which means that operators on the left are processed before operators on the right. The assignment operators and the conditional (?:) operator are right-associative, which means that the operators on the right are processed before operators on the left. For more information on assignment operators, see Using assignment operators. For more information on the conditional (?:) operator, see About the conditional operator.

For example, consider the less than (<) and greater than (>) operators, which have the same precedence. If both operators are used in the same expression, the operator on the left is processed first because both operators are left-associative. This means that the following two statements produce the same output:

trace(3 > 2 < 1);   // false
trace((3 > 2) < 1); // false

The greater than (>) operator is processed first, which results in a value of true because the operand 3 is greater than the operand 2. The value true is then passed to the less than (<) operator, along with the operand 1. The less than (<) operator converts the value true to the numeric value 1 and compares that numeric value to the second operand 1 to return the value false (the value 1 is not less than 1).

Consider the order of operands in your ActionScript, particularly when you set up complex conditions and you know how often one of those conditions is true. For example, if you know that i will be greater than 50 in your condition, you need to write i<50 first. Therefore, it's checked first, and the second condition that you write doesn't need to be checked as often.

The following table lists the operators for ActionScript 2.0 in order of decreasing precedence. Each row of the table contains operators of the same precedence. Each row of operators has higher precedence than the row appearing below it in the table. For more information and guidelines on using operators and parentheses, see Formatting ActionScript syntax.

Group

Operators

Primary

[] {x:y} () f(x) new x.y x[y]

Postfix

x++ x--

Unary

++x --x + - ~ ! delete typeof void

Multiplicative

* / %

Additive

+ -

Bitwise shift

<< >> >>>

Relational

< > <= >= instanceof

Equality

== != === !==

Bitwise AND

&

Bitwise XOR

^

Bitwise OR

|

Logical AND

&&

Logical OR

||

Conditional

?:

Assignment

= *= /= %= += -= <<= >>= >>>= &= ^= |=

Comma

,