JavaScript

JavaScript is a high-level, interpreted programming language that is widely used for web development. Initially designed as a client-side scripting language, it runs directly in web browsers, enabling dynamic and interactive user experiences. JavaScript can now be used for server-side development as well.

JavaScript Operator Precedence

12 April 2025 | Category:

Operator precedence determines the order in which operations are performed in an expression. Understanding precedence is crucial to writing correct and predictable JavaScript code.


1. What is Operator Precedence?

Operator precedence defines the order in which JavaScript evaluates different operators in an expression. When multiple operators appear in a single expression, those with higher precedence execute before those with lower precedence.

Example:

let result = 10 + 5 * 2;
console.log(result); // Output: 20 (Multiplication occurs before addition)

2. Associativity

Associativity defines the direction in which operators of the same precedence are processed. It can be left-to-right or right-to-left.

Example:

// Left-to-right (e.g., subtraction)
let result = 10 - 5 - 2;  // (10 - 5) - 2 = 3

// Right-to-left (e.g., assignment)
let a = b = c = 5; // c = 5, b = 5, a = 5

3. JavaScript Operator Precedence Table

Here’s a simplified table of JavaScript operators in order of precedence (from highest to lowest):

PrecedenceOperator(s)TypeAssociativity
1()GroupingN/A
2new (with argument list)Object creationRight-to-left
3++, -- (postfix)Postfix increment/decrementLeft-to-right
4++, -- (prefix), +, -, ~, !UnaryRight-to-left
5**ExponentiationRight-to-left
6*, /, %Multiplication/division/moduloLeft-to-right
7+, -Addition/subtractionLeft-to-right
8<<, >>, >>>Bitwise shiftLeft-to-right
9<, <=, >, >=, in, instanceofComparisonLeft-to-right
10==, !=, ===, !==EqualityLeft-to-right
11&Bitwise ANDLeft-to-right
12^Bitwise XORLeft-to-right
13``Bitwise OR
14&&Logical ANDLeft-to-right
15``
16??Nullish coalescingLeft-to-right
17?:Conditional (ternary)Right-to-left
18=, +=, -=, etc.AssignmentRight-to-left
19yieldYieldRight-to-left
20...Spread/restN/A
21,CommaLeft-to-right

4. Examples by Category

a. Grouping

let result = (2 + 3) * 4;  // 5 * 4 = 20

b. Unary and Exponentiation

let x = -3 ** 2; // Error: must use parentheses: -(3 ** 2)
let x = -(3 ** 2); // -9

c. Logical vs Assignment

let a = true && false;  // false
let b = false || true;  // true
let c = a = b;          // assignment after logical eval

d. Conditional Operator

let age = 20;
let status = age >= 18 ? "Adult" : "Minor";
console.log(status); // "Adult"

5. Best Practices

  • Use parentheses to make complex expressions more readable.
  • Know your operator types (unary, binary, ternary).
  • Avoid tricky combinations in a single line. Break them up.

6. Conclusion

Understanding JavaScript operator precedence helps prevent logic errors and write cleaner, more predictable code. While JavaScript handles precedence rules internally, being aware of them allows you to use parentheses effectively and control how your expressions are evaluated.

Always refer to the precedence table when dealing with complex expressions and make use of parentheses to explicitly define execution order.