3.9 The Predominance of Operators
When you work with complex expressions in JavaScript, you must keep operator precedence in mind. Each operator has its own priority level, and the engine evaluates higher-priority operators before lower-priority ones — exactly like in mathematics.
Consider this expression:
let x = 2 + 3 * 4;
console.log(x); // 14
The result is 14, not 20. Multiplication has higher precedence than addition, so 3 * 4 is evaluated first (= 12), and then 2 + 12 gives 14. Every operator in this section has its own priority, and memorizing them all is hard.
Forcing the order with parentheses
The simplest and safest way to make your intent explicit is to wrap parts of the expression in parentheses. They have the highest precedence, so the engine always evaluates their content first:
let x = (2 + 3) * 4;
console.log(x); // 20
- Parentheses also improve readability — they document the evaluation order for the next developer.
- When in doubt about precedence, add parentheses; the cost is zero.
That's it for this short demonstration on operator precedence in JavaScript. See you in the next video.
Summary
Operator precedence is a fundamental rule that determines the order in which operations are evaluated in complex mathematical expressions. This lesson demonstrates that multiplication has higher precedence than addition, using the example 2 + 3 × 4 = 14 (not 20). The instructor explains how parentheses can override default precedence, allowing precise control over evaluation order—for instance, (2 + 3) × 4 = 20 produces a different result by forcing addition to execute first.
Key points
- Operator precedence dictates which calculations execute first in complex expressions without parentheses
- Multiplication and division have higher priority than addition and subtraction, causing them to evaluate first
- Parentheses override default precedence and force the enclosed expression to be evaluated before surrounding operators
- Memorizing all precedence rules is impractical; use parentheses for clarity in complex or ambiguous expressions
- Incorrect precedence assumptions lead to logic errors—understanding these rules prevents bugs in your code
FAQ
What is operator precedence and why does 2 + 3 × 4 equal 14, not 20?
Operator precedence is the set of rules determining which operations execute first. Multiplication has higher precedence than addition, so 3 × 4 (= 12) is calculated before adding 2, yielding 14. Without this precedence rule, left-to-right evaluation would give 20.
How can I change the order of operations in an expression?
Use parentheses to override default precedence. For example, (2 + 3) × 4 forces addition to execute first (5), then multiplies by 4 to get 20—different from 2 + 3 × 4.
Why should I care about operator precedence if I can always use parentheses?
Understanding precedence prevents subtle logic errors and makes your intent clear to others reading the code. Relying entirely on parentheses for every expression is verbose; knowing the rules helps you write cleaner, more readable code.