Bagikan melalui


Operator Precedence

Operator precedence is a set of rules in JScript that controls the order in which the compiler performs operations when evaluating an expression. Operations with a higher precedence are performed before those with a lower one. For example, multiplication is performed before addition.

Precedence Table

The following table lists the JScript operators, ordered from highest to lowest precedence.

Precedence

Evaluation Order

Operator

Description

15

left to right

., [], ()

Field access, array indexing, function calls, and expression grouping

14

right to left

++, --, -, ~, !, delete, new, typeof, void

Unary operators, return data type, object creation, undefined values

13

left to right

*, /, %

Multiplication, division, modulo division

12

left to right

+, -

Addition and string concatenation, subtraction

11

left to right

<<, >>, >>>

Bit shifting

10

left to right

<, <=, >, >=, instanceof

Less than, less than or equal, greater than, greater than or equal, instanceof

9

left to right

==, !=, ===, !==

Equality, inequality, strict equality, and strict inequality

8

left to right

&

Bitwise AND

7

left to right

^

Bitwise XOR

6

left to right

|

Bitwise OR

5

left to right

&&

Logical AND

4

left to right

||

Logical OR

3

right to left

?:

Conditional

2

right to left

=, OP=

Assignment, compound assignment

1

left to right

, (comma)

Multiple evaluation

Parentheses in an expression alter the order of evaluation determined by operator precedence. This means that an expression within parentheses is fully evaluated before its value is used in the remainder of the expression.

For example:

z = 78 * (96 - 3 + 45)

There are five operators in the preceding expression: =, *, (), -, and +. According to the rules of operator precedence, they are evaluated in the following order: (), -, +, *, =.

  1. Evaluation of the expression within the parentheses occurs first. Within the parentheses, there is an addition operator and a subtraction operator. The operators both have the same precedence, and they are evaluated from left to right. The number 3 is subtracted from 96 first, resulting in 93. Then the number 45 is added to 93, resulting in a value of 138.

  2. Multiplication occurs next. The number 78 is multiplied by the number 138, resulting in a value of 10764.

  3. Assignment occurs last. The number 10764 is assigned to z.

See Also

Concepts

Operator Summary

Other Resources

JScript Operators