JavaScript Operator Precedence determines the order in which operators are evaluated when an expression contains multiple operators. Operators with higher precedence are evaluated before operators with lower precedence. For example, multiplication is performed before addition, so 10 + 5 * 2 produces 20, not 30. Understanding operator precedence helps developers predict how JavaScript evaluates expressions and avoid unexpected results. Parentheses can be used to explicitly control the order of evaluation and make complex expressions easier to understand.
Table of Contents
What is Operator Precedence in JavaScript?
Operator precedence is a set of rules that determines which operator JavaScript evaluates first when an expression contains multiple operators.
For example:
// JavaScript program to demonstrate operator precedence let result = 10 + 5 * 2; console.log(result);
20
The multiplication operation is performed first:
5 * 2 = 10
Then the addition is performed:
10 + 10 = 20
Therefore, the result is 20.
Why is Operator Precedence Important?
Operator precedence is important because expressions can contain several operators at the same time.
For example:
// JavaScript program to demonstrate why operator precedence is important let result = 20 - 5 * 2 + 10; console.log(result);
20
JavaScript evaluates multiplication first:
5 * 2 = 10
Then it evaluates subtraction and addition from left to right:
20 - 10 + 10 = 20
Without understanding precedence, it may be easy to incorrectly expect a different result.
How Does Operator Precedence Work?
JavaScript generally evaluates operators according to their precedence level.
- Higher Precedence First: Operators with higher precedence are evaluated before operators with lower precedence.
- Parentheses First: Expressions inside parentheses are evaluated before surrounding operations.
- Same Precedence: When operators have the same precedence, their associativity determines the evaluation direction.
- Lower Precedence Later: Operators with lower precedence are evaluated after higher-precedence operations.
Operator Precedence Example
Consider the following expression:
// JavaScript program to demonstrate operator precedence let result = 10 + 20 * 3; console.log(result);
70
JavaScript first evaluates:
20 * 3 = 60
Then:
10 + 60 = 70
Therefore, multiplication has higher precedence than addition.
Parentheses and Operator Precedence
Parentheses can be used to change the normal order of evaluation.
For example:
// JavaScript program to demonstrate parentheses with operator precedence let result = (10 + 20) * 3; console.log(result);
90
The expression inside the parentheses is evaluated first:
10 + 20 = 30
Then multiplication is performed:
30 * 3 = 90
Parentheses are useful when you want to make the intended calculation clear.
Arithmetic Operator Precedence
Arithmetic operators do not all have the same precedence.
A simplified order from higher to lower precedence is:
| Operator | Description | Precedence |
|---|---|---|
| ** | Exponentiation | Higher |
| * | Multiplication | High |
| / | Division | High |
| % | Remainder | High |
| + | Addition | Lower |
| - | Subtraction | Lower |
For example:
// JavaScript program to demonstrate arithmetic operator precedence let result = 10 + 5 * 2 - 4; console.log(result);
16
JavaScript first performs multiplication:
5 * 2 = 10
Then evaluates the remaining expression:
10 + 10 - 4 = 16
Comparison Operator Precedence
Comparison operators are generally evaluated after arithmetic expressions.
For example:
// JavaScript program to demonstrate comparison operator precedence let result = 10 + 5 > 12; console.log(result);
true
JavaScript first evaluates:
10 + 5 = 15
Then performs the comparison:
15 > 12
The result is true.
Logical Operator Precedence
Logical operators also have different precedence levels.
The general order among the common logical operators is:
! → && → ||
For example:
// JavaScript program to demonstrate logical operator precedence let result = true || false && false; console.log(result);
true
The && operation is evaluated before ||:
false && false = false
Then:
true || false = true
Assignment Operator Precedence
Assignment operators such as = have relatively low precedence compared with arithmetic and comparison operators.
For example:
// JavaScript program to demonstrate assignment operator precedence let result = 10 + 5 * 2; console.log(result);
20
The arithmetic operations are completed before the resulting value is assigned to result.
Assignment operators include:
=
+=
-=
*=
/=
%=
**=
Unary Operator Precedence
Unary operators operate on a single value and generally have higher precedence than arithmetic operators.
For example, unary minus is evaluated before multiplication.
// JavaScript program to demonstrate unary operator precedence let result = -5 * 2; console.log(result);
-10
The value 5 is first changed to -5, and then multiplication is performed.
The typeof operator is another unary operator.
// JavaScript program to demonstrate typeof operator precedence let result = typeof 10 + 20; console.log(result);
number20
Here, typeof 10 is evaluated first and produces "number". The + operator then concatenates "number" and 20.
Exponentiation and Operator Precedence
The exponentiation operator (**) has higher precedence than multiplication, division, addition, and subtraction.
// JavaScript program to demonstrate exponentiation precedence let result = 2 + 3 ** 2; console.log(result);
11
JavaScript first calculates:
3 ** 2 = 9
Then:
2 + 9 = 11
Increment and Decrement Precedence
The increment (++) and decrement (--) operators have high precedence.
They can be used in prefix or postfix form.
// JavaScript program to demonstrate increment operator precedence let number = 5; let result = ++number * 2; console.log(result); console.log(number);
12
6
The prefix increment increases the number before it is used in the multiplication.
Ternary Operator Precedence
The ternary operator (? :) has lower precedence than many arithmetic, comparison, and logical operators.
For example:
// JavaScript program to demonstrate ternary operator precedence let age = 20; let result = age >= 18 ? "Adult" : "Minor"; console.log(result);
Adult
The comparison is evaluated first:
age >= 18
Then the ternary operator selects the appropriate result.
Logical AND and OR Precedence
The logical AND operator (&&) has higher precedence than the logical OR operator (||).
For example:
// JavaScript program to demonstrate AND and OR precedence let result = false || true && false; console.log(result);
false
JavaScript evaluates && first:
true && false = false
Then:
false || false = false
Parentheses can be used when a different order is required.
// JavaScript program to demonstrate parentheses with logical operators let result = (false || true) && false; console.log(result);
false
?? and Operator Precedence
The nullish coalescing operator (??) has its own precedence rules and should be used carefully when combined with other logical operators.
JavaScript does not allow ?? to be directly mixed with || or && without parentheses.
For example, use:
// JavaScript program to demonstrate parentheses with nullish coalescing let value = null; let result = (value ?? 10) || 20; console.log(result);
10
Parentheses make the intended order of evaluation clear.
Operator Associativity
Operator precedence determines which operator is evaluated first, while associativity determines the direction in which operators with the same precedence are evaluated.
Most operators are left-associative.
For example:
// JavaScript program to demonstrate left-to-right associativity let result = 20 - 5 - 3; console.log(result);
12
The expression is evaluated from left to right:
20 - 5 = 15
Then:
15 - 3 = 12
Some operators, such as exponentiation and assignment, are right-associative.
For example:
// JavaScript program to demonstrate right-to-left associativity let result = 2 ** 3 ** 2; console.log(result);
512
The exponentiation is evaluated from right to left:
3 ** 2 = 9
Then:
2 ** 9 = 512
Complete Example of Operator Precedence
The following example contains multiple operators.
// JavaScript program to demonstrate multiple operator precedence rules let a = 10; let b = 5; let c = 2; let result = a + b * c > 15 && a > c; console.log(result);
true
JavaScript evaluates the expression in stages.
- Multiplication: b * c is evaluated first.
- Addition: The result is added to a.
- Comparison: The calculated value is compared with 15.
- Second Comparison: a > c is evaluated.
- Logical AND: The two Boolean results are combined using &&.
Common Operator Precedence Order
A simplified precedence order from higher to lower can be remembered as follows:
- Parentheses: Expressions inside parentheses.
- Member Access and Function Calls: Accessing properties and calling functions.
- Unary Operators: Operators such as !, typeof, unary +, and unary -.
- Exponentiation: **.
- Multiplication, Division, and Remainder: *, /, %.
- Addition and Subtraction: +, -.
- Comparison Operators: <, >, <=, >=.
- Equality Operators: ==, ===, !=, !==.
- Logical AND: &&.
- Logical OR: ||.
- Nullish Coalescing: ??.
- Ternary Operator:? :.
- Assignment Operators: =, +=, -=, *=, and others.
This is a simplified order for commonly used operators. JavaScript has a more detailed precedence hierarchy that includes additional operators.
Advantages of Understanding Operator Precedence
- Prevents Calculation Errors: Understanding precedence helps predict the correct result of complex expressions.
- Improves Code Understanding: It makes existing JavaScript expressions easier to read.
- Reduces Unnecessary Parentheses: Simple expressions can be written correctly without adding unnecessary parentheses.
- Helps with Logical Expressions: It makes combinations of &&, ||, !, and comparison operators easier to understand.
- Improves Debugging: Knowing evaluation order makes it easier to find unexpected results.
- Supports Better Code: Developers can write expressions with a clear understanding of how JavaScript evaluates them.
Common Beginner Mistakes
- Assuming Operators Are Evaluated Strictly from Left to Right: JavaScript follows precedence rules, so higher-precedence operators are evaluated first.
- Forgetting Parentheses: Complex expressions can become difficult to understand when the intended order is not made explicit.
- Confusing Precedence with Associativity: Precedence decides which operator has priority, while associativity determines the direction for operators with the same precedence.
- Assuming || and && Have the Same Precedence: The && operator has higher precedence than ||.
- Mixing ?? with || or && Without Parentheses: JavaScript does not allow these operators to be directly mixed without parentheses.
Conclusion
JavaScript Operator Precedence determines the order in which operators are evaluated in an expression. Operators with higher precedence are generally evaluated before operators with lower precedence, while associativity determines the evaluation direction when operators have the same precedence. Parentheses can be used to explicitly control the evaluation order and make complex expressions easier to understand. A good understanding of operator precedence helps developers write correct, predictable, and readable JavaScript code.
FAQs
1. What is operator precedence in JavaScript?2. Which has higher precedence, multiplication or addition?Operator precedence is the set of rules that determines the order in which JavaScript evaluates operators in an expression.
3. Can parentheses change operator precedence?Multiplication has higher precedence than addition, so multiplication is evaluated first.
4. What is the difference between precedence and associativity?Yes. Parentheses force the expression inside them to be evaluated before the surrounding operations.
5. Why is operator precedence important in JavaScript?Precedence determines which operator is evaluated first, while associativity determines the evaluation direction when operators have the same precedence.
It helps developers understand how expressions are evaluated, avoid unexpected results, and write correct and readable code.
0 Comments