Operator precedence determines the order in which operators are evaluated in an expression. Operators with higher precedence are evaluated before those with lower precedence.
Click to reveal answer
beginner
Which operator has higher precedence: multiplication (*) or addition (+)?
Multiplication (*) has higher precedence than addition (+), so it is evaluated first in an expression.
Click to reveal answer
beginner
How does parentheses affect operator precedence?
Parentheses have the highest precedence and force the expression inside them to be evaluated first, regardless of other operators.
Click to reveal answer
beginner
What is the result of the expression: 3 + 4 * 2?
The multiplication happens first: 4 * 2 = 8. Then addition: 3 + 8 = 11. So, the result is 11.
Click to reveal answer
beginner
True or False: The assignment operator (=) has higher precedence than addition (+).
False. The addition operator (+) has higher precedence than the assignment operator (=). So addition happens before assignment.
Click to reveal answer
Which operator is evaluated first in the expression: 5 + 3 * 2?
AMultiplication (*)
BAddition (+)
CLeft to right
DAssignment (=)
✗ Incorrect
Multiplication (*) has higher precedence than addition (+), so it is evaluated first.
What does parentheses do in an expression?
AThey lower operator precedence
BThey force evaluation inside them first
CThey change operator types
DThey have no effect
✗ Incorrect
Parentheses have the highest precedence and force the expression inside to be evaluated first.
Which operator has the lowest precedence?
AMultiplication (*)
BAddition (+)
CAssignment (=)
DExponentiation (**)
✗ Incorrect
Assignment (=) has lower precedence than arithmetic operators like +, *, and **.
What is the result of: (2 + 3) * 4?
A5
B14
C24
D20
✗ Incorrect
Parentheses evaluated first: 2 + 3 = 5, then multiplied by 4 = 20.
In the expression 10 - 4 + 2, which operator is evaluated first?
ASubtraction (-)
BAddition (+)
CBoth at the same time
DDepends on parentheses
✗ Incorrect
Subtraction and addition have the same precedence and are evaluated left to right, so subtraction happens first.
Explain how operator precedence affects the evaluation of the expression 8 + 2 * 5.
Think about which operation happens first without parentheses.
You got /3 concepts.
Describe the role of parentheses in changing the order of operations in JavaScript expressions.
Consider how parentheses can change 2 + 3 * 4 to (2 + 3) * 4.
You got /3 concepts.
Practice
(1/5)
1. Which operator has the highest precedence in the expression 3 + 4 * 2?
easy
A. Multiplication (*)
B. Addition (+)
C. Subtraction (-)
D. Division (/)
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 A
Quick 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
A. 5 + (3 * 2)
B. 5 + * 3
C. 5 + 3 * )2(
D. 5 + 3 * 2)
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 A
Quick 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
A. 8
B. 12
C. 6
D. 10
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 C
Quick 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
A. No error, expression is valid
B. Incorrect use of parentheses
C. Division operator should be replaced with subtraction
D. Missing operand between * and /
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 D
Quick 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
A. 13
B. 10
C. 11
D. 14
Solution
Step 1: Evaluate parentheses and exponentiation first