Assignment operators in Javascript - Time & Space Complexity
Let's see how fast or slow assignment operators run in JavaScript.
We want to know how the time to do assignments changes when we have more data.
Analyze the time complexity of the following code snippet.
let total = 0;
for (let i = 0; i < arr.length; i++) {
total += arr[i];
}
This code adds up all numbers in an array using the += assignment operator inside a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The assignment with addition (total += arr[i]) inside the loop.
- How many times: Once for each item in the array (arr.length times).
As the array gets bigger, the number of assignments grows in the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 assignments |
| 100 | 100 assignments |
| 1000 | 1000 assignments |
Pattern observation: The work grows directly with the number of items.
Time Complexity: O(n)
This means the time to finish grows in a straight line as the input size grows.
[X] Wrong: "Assignment operators take constant time no matter what, so loops don't affect time."
[OK] Correct: Each assignment inside a loop happens many times, so total time depends on how many times the loop runs.
Understanding how assignment inside loops affects time helps you explain code efficiency clearly and confidently.
"What if we replaced the for loop with a nested loop doing assignments inside? How would the time complexity change?"
Practice
+= do in JavaScript?Solution
Step 1: Understand the
The+=operator+=operator adds the value on the right to the variable on the left.Step 2: Effect on the variable
It updates the left variable with the new sum, combining addition and assignment.Final Answer:
Adds the right value to the left variable and updates the variable -> Option AQuick Check:
x += ymeansx = x + y[OK]
- Confusing += with simple assignment =
- Thinking it subtracts or multiplies
- Believing it only adds without updating
num by 5 and update it using an assignment operator?Solution
Step 1: Identify the correct multiplication assignment syntax
The correct operator to multiply and assign is*=, placed between variable and value.Step 2: Check each option
num *= 5; usesnum *= 5;which is valid syntax. Others have incorrect operator placement or invalid syntax.Final Answer:
num *= 5; -> Option CQuick Check:
Use*=for multiply and assign [OK]
- Writing =* instead of *=
- Adding spaces inside operator
- Using invalid symbols like 5*;
let x = 10; x -= 3; console.log(x);
Solution
Step 1: Understand the initial value and operation
Variablexstarts at 10. The operator-=subtracts 3 fromx.Step 2: Calculate the new value
10 - 3 = 7, soxbecomes 7.Final Answer:
7 -> Option BQuick Check:
x -= 3meansx = x - 3[OK]
- Adding instead of subtracting
- Printing old value before update
- Confusing -= with =-
let count = 5; count +== 2; console.log(count);
Solution
Step 1: Identify the operator used
The code uses+==which is not a valid assignment operator in JavaScript.Step 2: Correct operator usage
The correct operator to add and assign is+=. So it should becount += 2;.Final Answer:
The operator +== is invalid syntax -> Option DQuick Check:
Use+=not+==for addition assignment [OK]
- Using double equals with + (like +==)
- Confusing console.log with other functions
- Ignoring semicolon errors (usually optional)
result? let result = 2; result *= 3; result += 4; result /= 2;
Solution
Step 1: Multiply result by 3
Starting withresult = 2, afterresult *= 3, result becomes 6.Step 2: Add 4 to result
Afterresult += 4, result becomes 6 + 4 = 10.Step 3: Divide result by 2
Afterresult /= 2, result becomes 10 / 2 = 5.Final Answer:
5 -> Option AQuick Check:
2 * 3 + 4 then / 2 = 5 [OK]
- Doing operations in wrong order
- Forgetting to update variable after each step
- Mixing up division and multiplication
Ready to prove it?
Pass the test and this pattern moves unknown
