Why Precedence Matters
Operator precedence decides which parts of an expression run first. Misunderstanding it can flip results.
When in doubt, parentheses make your intent explicit.
JavaScript Tutorial
Operator precedence determines the order in which JavaScript evaluates expressions.
Knowing the common rules and using parentheses prevents logic bugs and keeps code clear.
Complex expressions can behave differently than you expect. A small precedence mistake can flip a condition or calculation.
Parentheses make intent explicit and help teammates read your code quickly.
// Use parentheses to control order
(a + b) * c
a && b || c
a = b = cconsole.log(2 + 3 * 4); // 14
console.log((2 + 3) * 4); // 20Multiplication happens before addition unless you use parentheses.
console.log(true || false && false); // true
console.log((true || false) && false); // false&& runs before ||, so use parentheses when mixing them.
Operator precedence decides which parts of an expression run first. Misunderstanding it can flip results.
When in doubt, parentheses make your intent explicit.
Operators with the same precedence are evaluated based on associativity (left-to-right or right-to-left).
For example, assignment is right-to-left, while addition is left-to-right.
Multiplication and division come before addition and subtraction.
Logical AND (&&) runs before logical OR (||).
Ternary has lower precedence than logical operators but higher than assignment.
Use parentheses to document intent, especially when mixing multiple operator types.
Readable expressions reduce bugs and make code review easier.
let a, b, c;
a = b = c = 5;
console.log(a, b, c); // 5 5 5Assignment is right-associative, so it evaluates from right to left.
const score = 70;
const label = score >= 60 && score <= 100 ? "Pass" : "Fail";
console.log(label);Use parentheses if the ternary condition gets complex.
Without
const result = 2 + 3 * 4; // 14With
const result = (2 + 3) * 4; // 20Check associativity for operators like assignment and exponentiation.
Parentheses make intent clear and prevent logic bugs.
`**` has higher precedence and is right-associative.
Break them into intermediate variables for readability.
It is the order JavaScript uses to evaluate operators.
It is the direction of evaluation for operators of the same precedence.
Use parentheses or break expressions into variables.
Practice: Evaluate a mixed expression and then rewrite it with parentheses to make the order explicit.
const value = 5 + 2 * 3 > 10 || false && true;
// TODO: rewrite with parentheses
One Possible Solution
const value = (5 + (2 * 3) > 10) || (false && true);
console.log(value);It defines the order in which operators are evaluated in an expression.
It defines the direction of evaluation when operators have the same precedence.
No. Know the common rules and use parentheses for clarity.
Exponentiation is right-associative, so it evaluates as 2 ** (3 ** 2).
Run the precedence examples and add parentheses to see the difference.