Operator precedence tells JavaScript which part of a math or logic problem to solve first. It helps avoid confusion and mistakes.
Operator precedence in Javascript
Introduction
Syntax
Javascript
/* Example of operator precedence in JavaScript */ let result = 3 + 4 * 2; // Multiplication (*) happens before addition (+) console.log(result);
Operators like * and / have higher precedence than + and -.
Use parentheses () to change the order if needed.
Examples
Javascript
let a = 5 + 3 * 2; console.log(a);
Javascript
let b = (5 + 3) * 2; console.log(b);
Javascript
let c = true || false && false; console.log(c);
Javascript
let d = !false && true; console.log(d);
Sample Program
This program shows how operator precedence affects the results of calculations and logic expressions.
Javascript
let x = 10 + 5 * 2; let y = (10 + 5) * 2; let z = !false || false && true; console.log('x =', x); console.log('y =', y); console.log('z =', z);
Important Notes
Remember: multiplication and division happen before addition and subtraction.
Logical NOT (!) happens before AND (&&), which happens before OR (||).
Use parentheses to make your code clear and control the order explicitly.
Summary
Operator precedence decides which operations run first in expressions.
Multiplication and division have higher precedence than addition and subtraction.
Use parentheses to change or clarify the order of operations.
Practice
1. Which operator has the highest precedence in the expression
3 + 4 * 2?easy
Solution
Step 1: Recall operator precedence rules
Multiplication and division have higher precedence than addition and subtraction.Step 2: Identify operators in the expression
The expression has addition (+) and multiplication (*). Multiplication runs first.Final Answer:
Multiplication (*) -> Option AQuick Check:
Multiplication > Addition [OK]
Hint: Multiplication/division run before addition/subtraction [OK]
Common Mistakes:
- Thinking addition runs before multiplication
- Ignoring operator precedence
- Assuming left-to-right always applies
2. Which of the following expressions is syntactically correct in JavaScript?
easy
Solution
Step 1: Check each expression for syntax errors
5 + * 3 has two operators in a row (+ *), which is invalid. 5 + 3 * )2( has misplaced parentheses. 5 + 3 * 2) has an unmatched closing parenthesis.Step 2: Confirm correct syntax
5 + (3 * 2) uses parentheses correctly and has valid operators and operands.Final Answer:
5 + (3 * 2) -> Option AQuick Check:
Valid parentheses and operators [OK]
Hint: Check parentheses and operator placement carefully [OK]
Common Mistakes:
- Misplacing parentheses
- Using two operators together
- Unmatched parentheses
3. What is the output of the following code?
console.log(10 - 2 * 3 + 4 / 2);
medium
Solution
Step 1: Apply operator precedence
Multiplication (*) and division (/) run before addition (+) and subtraction (-). Calculate 2 * 3 = 6 and 4 / 2 = 2 first.Step 2: Calculate remaining expression
Expression becomes 10 - 6 + 2. Calculate left to right: 10 - 6 = 4, then 4 + 2 = 6.Final Answer:
6 -> Option CQuick Check:
10 - 6 + 2 = 6 [OK]
Hint: Do * and / before + and -, then left to right [OK]
Common Mistakes:
- Adding before multiplying
- Ignoring division precedence
- Calculating right to left
4. Find the error in this expression:
let result = 5 + 3 * / 2;
medium
Solution
Step 1: Analyze the operator sequence
The expression has '* /' operators together without a number between them, which is invalid syntax.Step 2: Identify the syntax error
Every operator needs operands on both sides. Here, the '*' operator is missing its right operand before the '/' operator.Final Answer:
Missing operand between * and / -> Option DQuick Check:
Operators need operands on both sides [OK]
Hint: Operators must have numbers on both sides [OK]
Common Mistakes:
- Ignoring missing operand
- Assuming expression is valid
- Confusing operator roles
5. Given the expression
let x = 4 + 3 * 2 ** 2 / (1 + 1);, what is the value of x?hard
Solution
Step 1: Evaluate parentheses and exponentiation first
Parentheses: (1 + 1) = 2. Exponentiation: 2 ** 2 = 4.Step 2: Calculate multiplication and division left to right
Expression becomes 4 + 3 * 4 / 2. Calculate 3 * 4 = 12, then 12 / 2 = 6.Step 3: Perform addition
Finally, 4 + 6 = 10.Final Answer:
10 -> Option BQuick Check:
Parentheses & exponentiation first, then * /, then + [OK]
Hint: Do parentheses, then **, then * /, then + [OK]
Common Mistakes:
- Ignoring exponentiation precedence
- Doing addition before multiplication
- Miscomputing parentheses
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
