Operator precedence in Javascript - Time & Space Complexity
When we look at operator precedence, we want to understand how the order of operations affects the steps a program takes.
We ask: how does the program decide which operation to do first, and does that change how long it takes?
Analyze the time complexity of the following code snippet.
const a = 5 + 3 * 2;
const b = (5 + 3) * 2;
const c = 10 / 2 + 7;
const d = 10 / (2 + 7);
This code uses different operators with and without parentheses to show how operator precedence works.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Simple arithmetic operations (+, *, /) executed once each.
- How many times: Each operation runs only once, no loops or repeated steps.
Since each operation runs once, the total steps stay the same no matter the numbers used.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 4 operations |
| 100 | 4 operations |
| 1000 | 4 operations |
Pattern observation: The number of operations does not grow with input size; it stays constant.
Time Complexity: O(1)
This means the time to run these operations stays the same no matter what numbers we use.
[X] Wrong: "More complex expressions always take longer to run because of operator precedence."
[OK] Correct: Operator precedence only changes the order of operations, not how many operations happen or how long they take.
Understanding operator precedence helps you read and write code clearly and predict how it runs, a skill useful in many coding situations.
"What if we added a loop that repeats the expression 100 times? How would the time complexity change?"
Practice
3 + 4 * 2?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]
- Thinking addition runs before multiplication
- Ignoring operator precedence
- Assuming left-to-right always applies
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]
- Misplacing parentheses
- Using two operators together
- Unmatched parentheses
console.log(10 - 2 * 3 + 4 / 2);
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]
- Adding before multiplying
- Ignoring division precedence
- Calculating right to left
let result = 5 + 3 * / 2;
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]
- Ignoring missing operand
- Assuming expression is valid
- Confusing operator roles
let x = 4 + 3 * 2 ** 2 / (1 + 1);, what is the value of x?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]
- Ignoring exponentiation precedence
- Doing addition before multiplication
- Miscomputing parentheses
Ready to prove it?
Pass the test and this pattern moves unknown
