Die Operator Rangfolge bestimmt in welcher Reihenfolge Operatoren ausgeführt werden. Operatoren, die in der Rangfolge zuerst kommen werden auch zuerst ausgeführt. Hier ein bekanntes Beispiel:
3 + 4 * 5 // gibt 23 zurück
Da der Multiplikationsoperator in der Rangfolge vor dem Additionsoperator kommt wird er auch zuerst ausgeführt ("Punkt vor Strich").
Assoziativität
Assoziativität bestimmt in welcher Reihenfolge Operatoren der selben Rangfolge ausgeführt werden. Betrachten wir als Beispiel den folgenden Ausdruck:
a OP b OP c
Left-associativity (left-to-right oder links-nach-rechts) bedeutet, dass der Code als (a OP b) OP c ausgeführt wird
, während right-associativity (right-to-left oder rechts-nach-links) heißt, dass der Code wie a OP (b OP c) ausgeführt wird
. Zuordnungs Operatoren sind right-associative, also kann man schreiben:
a = b = 5;
mit dem erwarteten Resultat, dass sowohl a als auch b 5 sind. Das liegt daran, dass Zuordnungsopertoren den Wert zurükgeben, den sie zuerst zugeordnet haben. Zuerst wird b auf 5 gesetzt. Daraufhin wird a auf 5, also den Rückgabewert von b, gesetzt.
Tabelle
Die nachfolgende Tabelle ordnet die Operatoren nach ihrer precedence.
Precedence | Operator type | Associativity | Individual operators |
---|---|---|---|
20 | Grouping |
n/a | ( … ) |
19 | Member Access |
left-to-right | … . … |
Computed Member Access |
left-to-right | … [ … ] |
|
new (with argument list) |
n/a | new … ( … ) |
|
18 | Function Call | left-to-right | … ( … ) |
new (without argument list) |
right-to-left | new … |
|
17 | Postfix Increment |
n/a | … ++ |
Postfix Decrement |
n/a | … -- |
|
16 | Logical NOT | right-to-left | ! … |
Bitwise NOT | right-to-left | ~ … |
|
Unary Plus | right-to-left | + … |
|
Unary Negation | right-to-left | - … |
|
Prefix Increment | right-to-left | ++ … |
|
Prefix Decrement | right-to-left | -- … |
|
typeof | right-to-left | typeof … |
|
void | right-to-left | void … |
|
delete | right-to-left | delete … |
|
15 | Exponentiation | right-to-left | … ** … |
14 | Multiplication | left-to-right | … * … |
Division | left-to-right | … / … |
|
Remainder | left-to-right | … % … |
|
13 | Addition | left-to-right | … + … |
Subtraction | left-to-right | … - … |
|
12 | Bitwise Left Shift | left-to-right | … << … |
Bitwise Right Shift | left-to-right | … >> … |
|
Bitwise Unsigned Right Shift | left-to-right | … >>> … |
|
11 | Less Than | left-to-right | … < … |
Less Than Or Equal | left-to-right | … <= … |
|
Greater Than | left-to-right | … > … |
|
Greater Than Or Equal | left-to-right | … >= … |
|
in | left-to-right | … in … |
|
instanceof | left-to-right | … instanceof … |
|
10 | Equality | left-to-right | … == … |
Inequality | left-to-right | … != … |
|
Strict Equality | left-to-right | … === … |
|
Strict Inequality | left-to-right | … !== … |
|
9 | Bitwise AND | left-to-right | … & … |
8 | Bitwise XOR | left-to-right | … ^ … |
7 | Bitwise OR | left-to-right | … | … |
6 | Logical AND | left-to-right | … && … |
5 | Logical OR | left-to-right | … || … |
4 | Conditional | right-to-left | … ? … : … |
3 | Assignment | right-to-left | … = … |
… += … |
|||
… -= … |
|||
… **= … |
|||
… *= … |
|||
… /= … |
|||
… %= … |
|||
… <<= … |
|||
… >>= … |
|||
… >>>= … |
|||
… &= … |
|||
… ^= … |
|||
… |= … |
|||
2 | yield | right-to-left | yield … |
yield* | right-to-left | yield* … |
|
1 | Spread | n/a | ... … |
0 | Comma / Sequence | left-to-right | … , … |