The operator precedence along with their associativity determines how Typescript evaluates an expression when there are multiple Typescript Operators present in the expression. Each Typescript operators have certain precedence. The Typescript evaluates the operators with higher precedence first. If a group of operators has the same Precedence, then it evaluates them either left to right or right to left, depending on the operator’s associativity. The table end of the article. has a list of all operators, with their precedence along with associativity.
Table of Contents
Operator Precedence
For Example, take an simple arithmetic operation.
1 2 3 | 100+100*20 |
If we did the math from left to right and without any precedence we would add 100+100=200, and then multiply it 20. The answer would be 4000. But that is not what we get.
Multiplication always has higher precedence than an addition. Hence it performs the multiplication first (100*20=2000) and then the addition (100+2000=2100)
1 2 3 | console.log(100+100*20); //2100 |
The Parenthesis or (grouping) always has higher precedence than all other operators. Hence in the following expression evaluates (100+100) first and then multiplication.
1 2 3 | console.log((100+100)*20); //4000 |
Operators Associativity
The associativity defines the direction in which the evaluation takes place. It can be either left to right (left-associativity) or right to left (right associativity).
- Associativity comes into the picture only when the operators have the same precedence.
- All operators with the same precedence have the same associativity
The assignment Operator =
has a right to left associativity.
y=10
is evaluated first.y
becomes 10- x=y is next. Hence
x
becomes 10.
1 2 3 4 | y=5 x = y = 10 |
Note that in the above example if =
had left to right associativity, then the value of x will be 5.
Multiplication, division & reminder has the same Precedence and left-to-right associativity. The expression 10/5*20/8
is evaluated in the following order.
- 10/5 = 2
- 2*20 = 40
- 40/8 = 5
Parenthesis (grouping) and short-circuiting
The Parenthesis or grouping operator has the highest precedence. However, even they might not be evaluated, when they are part of the expression involving short-circuited Operators.
The bitwise operators like || & &&
stops the evaluation, if they find the match This is known as a short-circuiting. For Example in the code below does not invoke alert("Hello")
even with a parenthesis.
Similarly in expression, a || (b * c)
b*c
never invoked if a
is truthy.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | console.log(true || (alert("Hello"))) //alert is never evaluated let a =10 let b = 5 let c = 5 console.log(a || (b * c)); //10 b*c never invoked |
The logical AND, nullish-coalescing, optional chaining, and the conditional operator are the other short-circuited operators.
Operator Precedence & Associativity Table
Precedence | Operator type | Associativity | Individual operators |
---|---|---|---|
21 | Grouping | n/a | ( … ) |
20 | Member Access | left-to-right | … . … |
Computed Member Access | left-to-right | … [ … ] | |
new (with argument list) | n/a | new … ( … ) | |
Function Call | left-to-right | … ( … ) | |
Optional chaining | left-to-right | ?. | |
19 | new (without argument list) | right-to-left | new … |
18 | Postfix Increment | n/a | … ++ |
Postfix Decrement | … -- | ||
17 | Logical NOT | right-to-left | ! … |
Bitwise NOT | ~ … | ||
Unary Plus | + … | ||
Unary Negation | - … | ||
Prefix Increment | ++ … | ||
Prefix Decrement | -- … | ||
typeof | typeof … | ||
void | void … | ||
delete | delete … | ||
await | await … | ||
16 | Exponentiation | right-to-left | … ** … |
15 | Multiplication | left-to-right | … * … |
Division | … / … | ||
Remainder | … % … | ||
14 | Addition | left-to-right | … + … |
Subtraction | … - … | ||
13 | Bitwise Left Shift | left-to-right | … << … |
Bitwise Right Shift | … >> … | ||
Bitwise Unsigned Right Shift | … >>> … | ||
12 | Less Than | left-to-right | … < … |
Less Than Or Equal | … <= … | ||
Greater Than | … > … | ||
Greater Than Or Equal | … >= … | ||
in | … in … | ||
instanceof | … instanceof … | ||
11 | Equality | left-to-right | … == … |
Inequality | … != … | ||
Strict Equality | … === … | ||
Strict Inequality | … !== … | ||
10 | Bitwise AND | left-to-right | … & … |
9 | Bitwise XOR | left-to-right | … ^ … |
8 | Bitwise OR | left-to-right | … | … |
7 | Logical AND | left-to-right | … && … |
6 | Logical OR | left-to-right | … || … |
5 | Nullish coalescing operator | left-to-right | … ?? … |
4 | Conditional | left-to-right | … ? … : … |
3 | Assignment | right-to-left | … = … |
… += … | |||
… -= … | |||
… **= … | |||
… *= … | |||
… /= … | |||
… %= … | |||
… <<= … | |||
… >>= … | |||
… >>>= … | |||
… &= … | |||
… ^= … | |||
… |= … | |||
… &&= … | |||
… ||= … | |||
… ??= … | |||
2 | yield | right-to-left | yield … |
yield* | yield* … | ||
1 | Comma / Sequence | left-to-right | … , … |
Reference
Read More
- Complete Typescript Tutorial
- Typescript Operators
- Arithmetic Operators
- Unary plus / Unary minus Operators
- Increment/Decrement Operators
- Comparison / Relational Operators
- Equality Operator / Strict Equality Operators
- Ternary Conditional Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Nullish coalescing operator
- Comma Operator in Typescript
- Operator Precedence