In JavaScript, operator precedence determines the order in which operators are evaluated in an expression. Associativity defines how operators of the same precedence are evaluated.
1. Operator Precedence
Operator precedence refers to the order in which operators are executed in an expression. Operators with higher precedence are executed first.
Operator Precedence Order (from highest to lowest):
- Grouping
()
- Member Access
.
- Function Call
()
- Unary
++
,--
,+
,-
,~
,!
- Multiplication, Division, Modulus
*
,/
,%
- Addition, Subtraction
+
,-
- Relational
<
,<=
,>
,>=
- Equality
==
,===
,!=
,!==
- Logical AND
&&
- Logical OR
||
- Assignment
=
,+=
,-=
, etc. - Comma
,
(lowest precedence)
Example:
let result = 3 + 4 * 2; // 3 + (4 * 2) = 3 + 8 = 11 console.log(result); // 11
Explanation:
- The
*
operator has higher precedence than+
, so4 * 2
is calculated first, then added to3
.
2. Operator Associativity
When operators have the same precedence, associativity determines the order in which they are evaluated.
- Left-to-right associativity: Most operators (like
+
,-
,*
, etc.) are evaluated from left to right. - Right-to-left associativity: Assignment operators (
=
,+=
,-=
, etc.) and the ternary operator (? :
) are evaluated from right to left.
Example of Left-to-Right Associativity:
let result = 10 - 5 + 3; // (10 - 5) + 3 = 5 + 3 = 8 console.log(result); // 8
Explanation:
-
and+
have the same precedence and are evaluated from left to right, so10 - 5
is calculated first.
Example of Right-to-Left Associativity (Assignment):
let a = 10; let b = 20; let result = a = b = 5; // Right to left, b = 5, then a = 5 console.log(a, b); // 5 5
Explanation:
- The assignment operators are evaluated from right to left. So,
b = 5
happens first, followed bya = 5
.
3. Priority of Operations
When you have multiple operations in an expression, JavaScript follows the precedence and associativity rules to determine the order of evaluation.
Example:
let result = 2 + 3 * 4 - 6 / 2; console.log(result); // 2 + (3 * 4) - (6 / 2) = 2 + 12 - 3 = 11
Explanation:
- Multiplication (
*
) and division (/
) have higher precedence than addition (+
) and subtraction (-
), so they are evaluated first.
4. The Comma Operator
The comma operator allows you to evaluate multiple expressions, but it returns the value of the last expression.
Example:
let result = (1, 2, 3); console.log(result); // 3
Explanation:
- The expressions
1
,2
, and3
are evaluated from left to right, but the value of3
is returned.
JavaScript Operator Precedence & Associativity Chart
Operator | Description | Associativity | Example |
---|---|---|---|
() |
Grouping | Left-to-right | (3 + 4) * 2 |
. |
Member access | Left-to-right | obj.prop |
[] |
Computed member access | Left-to-right | arr[0] |
() |
Function call | Left-to-right | myFunc() |
++ , -- |
Postfix increment/decrement | None | x++ , x-- |
++ , -- |
Prefix increment/decrement | Right-to-left | ++x , --x |
! , ~ |
Logical/bitwise NOT | Right-to-left | !true , ~x |
* , / , % |
Multiplication, Division, Modulus | Left-to-right | 5 * 3 |
+ , - |
Addition, Subtraction | Left-to-right | 5 + 3 |
<< , >> , >>> |
Bitwise shift | Left-to-right | x << 2 |
< , <= , > , >= |
Relational | Left-to-right | x < y |
== , != , === , !== |
Equality | Left-to-right | x === y |
& |
Bitwise AND | Left-to-right | x & y |
^ |
Bitwise XOR | Left-to-right | x ^ y |
` | ` | Bitwise OR | Left-to-right |
&& |
Logical AND | Left-to-right | x && y |
` | ` | Logical OR | |
?: |
Conditional (Ternary) | Right-to-left | x ? y : z |
= |
Assignment | Right-to-left | x = 5 |
, |
Comma | Left-to-right | (a = 1, b = 2) |
Conclusion:
- Operator precedence determines the order of operations in an expression.
- Associativity decides the evaluation direction when operators have the same precedence.
- Use parentheses
()
to control the order of operations explicitly.