Unary operators in Javascript - Time & Space Complexity
Unary operators perform simple actions on a single value. We want to see how their use affects the time it takes for code to run.
How does the number of unary operations grow as input size changes?
Analyze the time complexity of the following code snippet.
const numbers = [1, 2, 3, 4, 5];
for (let i = 0; i < numbers.length; i++) {
numbers[i] = -numbers[i];
}
This code uses a unary minus operator to change each number to its negative inside a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Applying the unary minus operator to each element.
- How many times: Once for each element in the array.
Each element gets processed once, so the work grows directly with the number of elements.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 unary operations |
| 100 | 100 unary operations |
| 1000 | 1000 unary operations |
Pattern observation: Doubling the input doubles the number of unary operations.
Time Complexity: O(n)
This means the time to finish grows in a straight line with the number of items.
[X] Wrong: "Unary operators run instantly and don't add to time complexity."
[OK] Correct: Even simple operations take time, and when repeated many times, they add up proportionally to input size.
Understanding how simple operations inside loops affect performance helps you explain code efficiency clearly and confidently.
"What if we replaced the unary minus with a function call inside the loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand unary operator definition
A unary operator works with only one operand (value).Step 2: Identify unary operators in options
++ increments a single variable by 1, so it is unary. += and && require two operands, === compares two values.Final Answer:
++ -> Option AQuick Check:
Unary operator = ++ [OK]
- Confusing += as unary instead of binary
- Thinking && is unary when it's binary
- Mixing comparison operators with unary
x by 1 using a unary operator?Solution
Step 1: Recall decrement unary operator syntax
The decrement operator is written as -- before or after the variable.Step 2: Check each option for correct syntax
x-- is valid and decreases x by 1. x-= is incomplete syntax. --x- and x--x are invalid.Final Answer:
x-- -> Option BQuick Check:
Decrement syntax = x-- [OK]
- Using x-= instead of x--
- Adding extra characters like x--x
- Placing -- incorrectly like --x-
let a = 5;
console.log(-a);
Solution
Step 1: Understand unary minus operator
The unary minus operator (-) changes the sign of the number.Step 2: Apply unary minus to variable a
a is 5, so -a becomes -5.Final Answer:
-5 -> Option AQuick Check:
Unary minus of 5 = -5 [OK]
- Confusing unary minus with subtraction
- Expecting output 5 instead of -5
- Thinking it returns NaN or undefined
let count = 10;
count = ++;
Solution
Step 1: Analyze the use of ++ operator
The ++ operator must be used with a variable, either before or after it.Step 2: Identify the error in the code
Here, ++ is used alone without a variable, causing a syntax error.Final Answer:
Missing variable after ++ operator -> Option CQuick Check:
++ needs a variable to work [OK]
- Thinking ++ alone increments something
- Ignoring syntax errors from incomplete expressions
- Confusing missing semicolon as main error
result?let x = 3;
let y = ++x + x++ + --x;
let result = y;
Solution
Step 1: Evaluate ++x (pre-increment)
++x increases x from 3 to 4, returns 4.Step 2: Evaluate x++ (post-increment)
x++ returns current x (4), then increments x to 5.Step 3: Evaluate --x (pre-decrement)
--x decreases x from 5 to 4, returns 4.Step 4: Calculate y
y = 4 + 4 + 4 = 12.Step 5: Check final value of result
result = y = 12.Final Answer:
12 -> Option DQuick Check:
Pre/post increments affect value and order [OK]
- Ignoring order of increments and decrements
- Mixing pre- and post-increment results
- Calculating without updating variable values
Ready to prove it?
Pass the test and this pattern moves unknown
