Precedence & Associativity of Operators in ActionScript
Notes:
Precedence & Associativity of Operators in ActionScript Language:
Precedence:
While evaluating a given expression; which operator should be evaluated first before the other?
I.e. which operator needs to be given higher precedence than the other? is determined by precedence.
Associativity:
If same operator appears one or more times in an expression, in which direction the specific operator(s) need to be evaluated is determined by associativity.
a = 3 * 5 + 4 / 2 + 3 + 20 -10
Precedence and associativity of Action Script operators table:
Note: lower the precedence level number higher the precedence
P. Level Precedence
1 P: Parenthesis
2 U: Unary (Right to Left associativity)
3 M: Multiplicative
4 A: Additive
5 S: Shift
6 R: Relational
7 E: Equality
8 B: Bitwise
9 L: Logical
10 C: Conditional
11 A: Assignment (Right to Left associativity)
12 C: Comma
Example code:
var a:Number = 2 + 2 + 2;
trace(a); // 6
var b:Number = 2 + 3 * 5;
trace(b); // 17 not 25
var c:Number = 3 * 5 + 4 / 2 + 3;
trace(c); // 20 not 12.5
var d:Number = 3 * (5 +4) / 2 +3;
trace(d); // 16.5
var e:Number = 3 * 5 + 4 / 2 + 3 + 20 -10;
trace(e); // 30