Back to JavaScript
2026-04-175 min read

Operator precedence (JavaScript)

Learn Operator precedence (JavaScript) step by step with clear examples and exercises.

Title: Operator Precedence in JavaScript - Understanding and Avoiding Common Mistakes

Why This Matters

Operator precedence is a fundamental concept in JavaScript that determines the order in which expressions involving multiple operators are evaluated. Understanding operator precedence is crucial for writing efficient, bug-free code, especially when dealing with complex expressions. In interviews, you may be asked to solve problems that rely on a deep understanding of operator precedence. Additionally, real-world coding often involves complex expressions where incorrect operator precedence can lead to unexpected results and bugs.

Prerequisites

Before diving into operator precedence, it's essential to have a good understanding of the following topics:

  1. Basic JavaScript syntax, including variables, operators, and expressions.
  2. Control structures such as loops (for, while, do-while) and conditional statements (if, else if, else).
  3. Functions and function calls.
  4. Data types in JavaScript, including numbers, strings, booleans, arrays, and objects.
  5. Basic concepts of object-oriented programming in JavaScript, such as classes and inheritance.
  6. Understanding the difference between loose equality (==) and strict equality (===).
  7. Familiarity with JavaScript's data types and their conversion rules.

Core Concept

Operator Precedence Overview

In JavaScript, expressions can contain multiple operators. The order in which these operators are evaluated is determined by their precedence. Operators with higher precedence are evaluated before those with lower precedence. For example, consider the following expression:

let result = 3 + 4 * 5;

In this case, the multiplication operator (*) has a higher precedence than the addition operator (+), so the multiplication operation is performed first, resulting in 20. Then, the addition operation is carried out, giving us the final result of 23.

Operator Precedence Table

Here's a table listing the JavaScript operators from highest to lowest precedence:

  1. Postfix operators (++, --)
  2. Unary operators (+, -, !, typeof, delete, void, await)
  3. Multiplicative operators (*, /, %)
  4. Additive operators (+, -)
  5. Shift operators (<<, >>, >>>)
  6. Relational operators (<, <=, >, >=)
  7. Equality operators (==, !=, ===, !==)
  8. Bitwise AND operator (&)
  9. Bitwise XOR operator (^)
  10. Bitwise OR operator (|)
  11. Logical AND operator (&&)
  12. Logical OR operator (||)
  13. Conditional (ternary) operator (?:)
  14. Assignment operators (=, +=, -=, *=, /=, %=, <<=, >>=, >>>=, &=, ^=, |=)
  15. Comma operator (,)

Parentheses and Operator Precedence

Parentheses can be used to override the default operator precedence in JavaScript expressions. When an expression contains parentheses, the operators inside the parentheses are evaluated first, regardless of their precedence outside the parentheses. For example:

let result = (3 + 4) * 5; // Evaluates to 39

In this case, the addition operation is performed before the multiplication because of the parentheses, even though multiplication has a higher precedence than addition.

Worked Example

Let's consider an example that demonstrates operator precedence and how parentheses can affect the outcome:

let x = 3;
let y = 4;
let z = 5;

// Without parentheses
let result1 = x + y * z; // Evaluates to 27 (multiplication first)

// With parentheses
let result2 = (x + y) * z; // Evaluates to 39 (addition first)

In this example, the difference between using and not using parentheses results in a significant change in the final result. By understanding operator precedence and knowing when to use parentheses, you can write more accurate and efficient JavaScript code.

Common Mistakes

  1. ### Forgetting Parentheses

In complex expressions, forgetting to use parentheses can lead to unexpected results due to the default operator precedence. Always use parentheses when necessary to ensure that your code behaves as intended.

  1. ### Confusing Equality Operators

JavaScript has two types of equality operators: == (loose equality) and === (strict equality). The loose equality operator performs type coercion, which can lead to unexpected results. Use strict equality whenever possible to avoid these issues.

  1. ### Assuming Arithmetic Operator Precedence

While it's important to understand the precedence of arithmetic operators, remember that JavaScript follows a specific order of operations for other types of expressions as well. Familiarize yourself with the entire operator precedence table to avoid mistakes.

  1. ### Neglecting the Comma Operator

The comma operator (,) can be used to evaluate multiple expressions in a single statement, but it's often overlooked or misused. Use it carefully and only when necessary for readability and clarity.

  1. ### Misunderstanding Type Coercion

Type coercion occurs when JavaScript automatically converts one data type to another during an operation. Understanding how JavaScript performs type coercion can help you avoid unexpected results, especially when using the loose equality operator (==).

  1. ### Ignoring the Order of Evaluation for Conditional (ternary) Operator

The conditional (ternary) operator (?:) is evaluated from right to left. This means that the third operand is evaluated only if the second operand evaluates to true.

Practice Questions

  1. Given the variables a = 3, b = 4, and c = 5, what is the value of result in each of the following expressions:

a) result = a + b * c;

b) result = (a + b) * c;

  1. What are the differences between the loose equality operator (==) and the strict equality operator (===)? Provide an example demonstrating the difference.
  1. Write a JavaScript expression that calculates the factorial of a given number using recursion. Use parentheses to ensure correct precedence.
  1. Given the following code snippet:
let x = 10;
let y = 20;
let result = (x + y) * (x - y);
console.log(result); // Outputs 0

What is the order of operations in this expression, and why does it output 0? How can you modify the code to ensure that the correct result is calculated?

FAQ

### Why is it important to understand operator precedence in JavaScript?

Understanding operator precedence helps you write more accurate and efficient code by ensuring that expressions are evaluated correctly, even when they contain multiple operators. This understanding can also help you avoid common mistakes and bugs in your code.

### What happens if I forget parentheses in a complex expression?

If you forget parentheses in a complex expression, the default operator precedence will determine the order of operations, which might not be what you intended. This can lead to unexpected results or bugs in your code.

### How do I know when to use parentheses in JavaScript expressions?

Use parentheses whenever necessary to ensure that your code behaves as intended, especially in complex expressions where the default operator precedence might not be what you want. You should also use parentheses to make your code more readable and easier to understand.

### What is the difference between loose equality (==) and strict equality (===)?

Loose equality (==) performs type coercion, which means that it will convert operands to the same type before comparing them. Strict equality (===) does not perform type coercion and compares operands as they are. Using strict equality can help you avoid unexpected results due to type coercion.

### How does JavaScript determine the order of operations for expressions that don't contain parentheses?

JavaScript follows a specific order of operations, known as the BODMAS (Brackets, Orders, Multiplication and Division, Addition and Subtraction) rule. However, in JavaScript, this rule is often referred to as PEMDAS (Parentheses, Exponents, Multiplication and Division, Addition and Subtraction).

### What is the conditional (ternary) operator, and how is it evaluated?

The conditional (ternary) operator is a shorthand way of writing an if statement in JavaScript. It consists of three operands separated by question marks (?) and colons (:). The first operand is a condition that can be either true or false. If the condition is true, the second operand is evaluated; otherwise, the third operand is evaluated. The conditional operator is evaluated from right to left.

Operator precedence (JavaScript) | JavaScript | XQA Learn