Welcome to TheCodingCollege.com! When working with JavaScript, understanding operator precedence is crucial to writing correct and efficient code. Operator precedence determines the order in which operations are performed in expressions, especially when multiple operators are involved. Along with precedence, associativity determines how operators of the same precedence are evaluated.
In this guide, we’ll explain JavaScript operator precedence, provide examples, and help you master this fundamental concept.
What Is Operator Precedence?
Operator precedence is the set of rules that dictate the sequence in which operators are evaluated in an expression.
For example:
const result = 10 + 5 * 2; // Is it (10 + 5) * 2 or 10 + (5 * 2)?
console.log(result); // Output: 20
In the above example, the multiplication (*
) operator has higher precedence than addition (+
), so 5 * 2
is evaluated first, resulting in 10 + 10 = 20
.
Operator Precedence Table
JavaScript has many operators, grouped by their precedence level. Operators with higher precedence are evaluated before operators with lower precedence.
Complete Operator Precedence Table
Precedence | Operator Type | Operators | Associativity |
---|---|---|---|
20 | Grouping | () | N/A |
19 | Member Access | . | Left-to-right |
Computed Member Access | [] | Left-to-right | |
Function Call | () | Left-to-right | |
Optional Chaining | ?. | Left-to-right | |
18 | New (without arguments) | new | Right-to-left |
17 | Postfix Increment/Decrement | x++ , x-- | N/A |
16 | Logical NOT, Bitwise NOT | ! , ~ | Right-to-left |
Unary Plus/Minus | + , - | Right-to-left | |
Typeof, Void, Delete | typeof , void , delete | Right-to-left | |
15 | Exponentiation | ** | Right-to-left |
14 | Multiplication/Division/Modulus | * , / , % | Left-to-right |
13 | Addition/Subtraction | + , - | Left-to-right |
12 | Bitwise Shift | << , >> , >>> | Left-to-right |
11 | Relational | < , <= , > , >= , in , instanceof | Left-to-right |
10 | Equality | == , != , === , !== | Left-to-right |
9 | Bitwise AND | & | Left-to-right |
8 | Bitwise XOR | ^ | Left-to-right |
7 | Bitwise OR | ` | ` |
6 | Logical AND | && | Left-to-right |
5 | Logical OR | ` | |
Nullish Coalescing | ?? | Left-to-right | |
4 | Conditional | ? : | Right-to-left |
3 | Assignment | = , += , -= , etc. | Right-to-left |
2 | Yield, Spread | yield , ... | Right-to-left |
1 | Comma | , | Left-to-right |
Associativity: Left-to-Right vs. Right-to-Left
When operators have the same precedence, their associativity decides the order of evaluation:
- Left-to-Right Associativity: Operators are evaluated from left to right.
Example:
const result = 10 - 5 - 2; // (10 - 5) - 2
console.log(result); // Output: 3
- Right-to-Left Associativity: Operators are evaluated from right to left.
Example:
const result = 2 ** 3 ** 2; // 2 ** (3 ** 2)
console.log(result); // Output: 512
Examples of Operator Precedence
1. Arithmetic and Assignment
let x = 10;
x += 5 * 2; // Multiplication is evaluated before addition.
console.log(x); // Output: 20
2. Logical AND (&&
) vs. Logical OR (||
)
const a = true || false && false;
// AND is evaluated before OR.
console.log(a); // Output: true
3. Grouping with Parentheses
Parentheses have the highest precedence and can override default precedence rules.
const result = (10 + 5) * 2;
console.log(result); // Output: 30
4. Exponentiation (**
)
Exponentiation has higher precedence than multiplication or division but is right-associative.
const result = 2 ** 3 ** 2; // 2 ** (3 ** 2)
console.log(result); // Output: 512
5. Chaining Optional Properties
Optional chaining (?.
) takes precedence over most operations.
const user = { name: "Alice" };
console.log(user?.name?.toUpperCase()); // Output: ALICE
Best Practices for Handling Operator Precedence
- Use Parentheses: When in doubt, use parentheses to explicitly define the order of operations.
const result = (10 + 5) * 2; // Clear and intentional.
- Readability Over Cleverness: Avoid overly complex expressions that rely on precedence rules.
- Review Associativity: Understand left-to-right or right-to-left behavior to avoid unintended results.
- Test Your Code: Run examples to confirm your understanding of how precedence affects evaluation.
Why Learn JavaScript Operator Precedence at TheCodingCollege.com?
At TheCodingCollege.com, we break down complex programming concepts into simple, digestible lessons. You’ll find:
- Detailed Explanations: Learn operator precedence with real-world examples.
- Interactive Guides: Test and apply your knowledge directly in coding exercises.
- Expert Advice: Empower your coding journey with insights from professionals.
Conclusion
Understanding JavaScript operator precedence is vital for writing clean and error-free code. By mastering these rules, you can avoid bugs, optimize performance, and make your code more readable.