4. Precedence and Associativity in JavaScript Operators
Understanding the precedence and associativity of operators in JavaScript is crucial for writing correct and efficient code. Let's delve deeper into how these properties determine the order of operations in expressions.
Precedence
Precedence refers to the priority of operators in an expression. JavaScript assigns each operator a numerical value to indicate its precedence. Operators with higher precedence are evaluated first. If two operators have the same precedence, their associativity comes into play to determine the order of evaluation.
For example, in the expression a + b * c, the * operator has a higher precedence than the + operator. Therefore, b * c is evaluated first, and then the result is added to a.
Associativity
Associativity determines the order of evaluation when operators with the same precedence appear consecutively in an expression. Operators can be left-associative or right-associative.
- Left-associative: Operations are evaluated from left to right.
- Right-associative: Operations are evaluated from right to left.
For instance, in an expression with left-associative operators like a + b - c, the addition (+) and subtraction (-) operators have the same precedence. They are evaluated from left to right.
Operator Precedence Table
Let's take a closer look at the operator precedence table provided:
Precedence |
Operator |
Associativity |
Symbol |
14 |
Grouping |
n/a |
( … ) |
13 |
Field access |
|
… . … |
12 |
Function call |
|
… ( … ) |
11 |
Postfix increment |
n/a |
… ++ |
|
Postfix decrement |
n/a |
… -- |
10 |
Logical NOT |
|
! … |
|
Unary plus |
|
+ … |
|
Unary negation |
|
- … |
|
Prefix increment |
|
++ … |
|
Prefix decrement |
|
-- … |
|
typeof |
|
typeof … |
|
delete |
|
delete … |
9 |
Exponentiation |
|
… ** … |
8 |
Multiplication |
|
… * … |
|
Division |
|
… / … |
|
Remainder |
|
… % … |
7 |
Addition |
|
… + … |
|
Subtraction |
|
… - … |
6 |
Less than |
|
… < … |
|
Less than or equal |
|
… <= … |
|
Greater than |
|
… > … |
|
Greater than or equal |
|
… >= … |
|
instanceof |
|
… instanceof … |
5 |
Equality |
|
… == … |
|
Inequality |
|
… != … |
|
Strict Equality |
|
… === … |
|
Strict Inequality |
|
… !== … |
4 |
Logical AND |
|
… && … |
3 |
Logical Or |
|
… || … |
2 |
Conditional (ternary) |
|
… ? … : … |
1 |
Assignment |
|
… = … |
|
|
|
… += … |
|
|
|
… *= … |
|
|
|
… and other assignment operators |
In the table:
- Precedence: Indicates the priority of the operator. Higher numbers indicate higher precedence.
- Operator: Specifies the operator.
- Associativity: Indicates the direction of evaluation for operators with the same precedence.
- Symbol: Represents the symbol used for the operator.
Example
Let's revisit the example provided:
let a = 10;
let b = a + 2 * 3;
let c = a + 2 < 20 - 15;
console.log(a); // Output: 10
console.log(b); // Output: 16
console.log(c); // Output: false
In this example:
- b = a + 2 * 3: Multiplication (*) has a higher precedence than addition (+). Therefore, 2 * 3 is evaluated first, resulting in 6, which is then added to a, resulting in 16.
- c = a + 2 < 20 - 15: Both addition and subtraction operators have the same precedence, but addition is evaluated before comparison. So, a + 2 is calculated first, then 20 - 15, and finally, the comparison is made.
Understanding precedence and associativity helps ensure that expressions are evaluated correctly, leading to accurate program behavior.