JavaScript Precedence

In JavaScript, operator precedence determines the order in which operators are evaluated in an expression. When multiple operators appear in a single expression, the JavaScript engine uses precedence rules to decide which operator to execute first.

What is Operator Precedence?

Operator precedence defines the priority of operators in expressions. Operators with higher precedence are executed before those with lower precedence.

For example:

const result = 5 + 2 * 3; // Multiplication has higher precedence than addition
console.log(result); // 11

Associativity in JavaScript

When two operators have the same precedence, associativity determines the order of evaluation:

  • Left-to-Right Associativity: Operators are evaluated from left to right.
  • Right-to-Left Associativity: Operators are evaluated from right to left.

Example of Left-to-Right Associativity:

const result = 10 - 5 - 2; // Subtraction is left-to-right
console.log(result); // 3

Example of Right-to-Left Associativity:

const result = 2 ** 3 ** 2; // Exponentiation is right-to-left
console.log(result); // 512 (2 raised to the power of 9)

JavaScript Operator Precedence Table

Below is a list of JavaScript operators arranged in decreasing order of precedence.

PrecedenceOperator TypeOperatorsAssociativity
20Grouping( )N/A
19Member Access.Left-to-Right
18Function Call( )Left-to-Right
17Increment/Decrement++ (Post), -- (Post)Left-to-Right
16Logical NOT!, typeof, void, deleteRight-to-Left
15Exponentiation**Right-to-Left
14Multiplication*, /, %Left-to-Right
13Addition/Subtraction+, -Left-to-Right
12Relational<, <=, >, >=Left-to-Right
11Equality==, !=, ===, !==Left-to-Right
10Bitwise AND&Left-to-Right
9Bitwise OR``
8Logical AND&&Left-to-Right
7Logical OR`
6Conditional? :Right-to-Left
5Assignment=, +=, -=, *=, /=, etc.Right-to-Left
4Comma,Left-to-Right

Examples of Operator Precedence

1. Basic Example

const result = 10 + 2 * 5; 
// Multiplication (*) has higher precedence than addition (+).
console.log(result); // 20

2. Using Parentheses to Override Precedence

Parentheses ( ) have the highest precedence and can be used to control the order of operations.

const result = (10 + 2) * 5; 
// Parentheses ensure addition happens before multiplication.
console.log(result); // 60

3. Mixing Associativity

const result = 100 / 10 * 2; 
// Both division (/) and multiplication (*) have the same precedence.
// Evaluated left-to-right.
console.log(result); // 20

4. Right-to-Left Example

const result = 2 ** 3 ** 2;
// Exponentiation is evaluated right-to-left.
console.log(result); // 512

Operator Precedence in Logical Expressions

Logical operators (&&, | |, !) follow specific precedence rules:

  • Logical NOT ( ! ) has the highest precedence.
  • Logical AND (&&) is evaluated before Logical OR (| |).

Example:

const result = true || false && false; 
// AND (&&) is evaluated before OR (||).
console.log(result); // true

Practical Use Cases

1. Complex Calculations

Operator precedence simplifies expressions with multiple mathematical operations.

const result = (5 + 10) * (2 ** 3) / 4;
console.log(result); // 30

2. Conditional Checks

Using logical operators effectively requires understanding precedence.

const isEligible = (age >= 18 && hasLicense) || isSupervisor;

3. Assignments

Compound assignments like += have lower precedence.

let x = 10;
x += 5 * 2; // Multiplication is evaluated first.
console.log(x); // 20

Common Mistakes

Ignoring Parentheses:
Misunderstanding precedence can lead to unexpected results.

const result = 10 - 5 * 2; // Expected 10, but result is 0.
console.log(result); // 0

Fix:

const result = (10 - 5) * 2;
console.log(result); // 10

Overusing Parentheses:
Although parentheses clarify expressions, overuse can make code harder to read.

Assuming Associativity:
Always confirm the associativity of operators to avoid logic errors.

Leave a Comment