JavaScript Operator Precedence

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

PrecedenceOperator TypeOperatorsAssociativity
20Grouping()N/A
19Member Access.Left-to-right
Computed Member Access[]Left-to-right
Function Call()Left-to-right
Optional Chaining?.Left-to-right
18New (without arguments)newRight-to-left
17Postfix Increment/Decrementx++, x--N/A
16Logical NOT, Bitwise NOT!, ~Right-to-left
Unary Plus/Minus+, -Right-to-left
Typeof, Void, Deletetypeof, void, deleteRight-to-left
15Exponentiation**Right-to-left
14Multiplication/Division/Modulus*, /, %Left-to-right
13Addition/Subtraction+, -Left-to-right
12Bitwise Shift<<, >>, >>>Left-to-right
11Relational<, <=, >, >=, in, instanceofLeft-to-right
10Equality==, !=, ===, !==Left-to-right
9Bitwise AND&Left-to-right
8Bitwise XOR^Left-to-right
7Bitwise OR``
6Logical AND&&Left-to-right
5Logical OR`
Nullish Coalescing??Left-to-right
4Conditional? :Right-to-left
3Assignment=, +=, -=, etc.Right-to-left
2Yield, Spreadyield, ...Right-to-left
1Comma,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.

Leave a Comment