If statement in Javascript - Time & Space Complexity
We want to see how the time it takes to run an if statement changes as the input changes.
Does the if statement make the program slower when input grows?
Analyze the time complexity of the following code snippet.
function checkNumber(num) {
if (num > 10) {
return "Greater";
} else {
return "Smaller or equal";
}
}
This code checks if a number is greater than 10 and returns a message.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single if check (comparison).
- How many times: Exactly once per function call.
The if statement runs once no matter what the input number is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The number of operations stays the same even if the input number changes.
Time Complexity: O(1)
This means the if statement takes the same amount of time no matter how big or small the input is.
[X] Wrong: "If statements take longer when the input number is bigger."
[OK] Correct: The if check is just one comparison and does not repeat or grow with input size.
Understanding that simple if statements run in constant time helps you explain how your code behaves clearly and confidently.
"What if we added a loop inside the if statement? How would the time complexity change?"
Practice
if statement do in JavaScript?Solution
Step 1: Understand the purpose of an if statement
An if statement checks a condition and runs code only when that condition is true.Step 2: Compare options with the definition
Only Runs code only if a condition is true matches this behavior exactly.Final Answer:
Runs code only if a condition is true -> Option DQuick Check:
If statement = runs code if true [OK]
- Confusing if with loops
- Thinking if declares variables
- Mixing if with function definitions
if statement in JavaScript?Solution
Step 1: Recall JavaScript if syntax
The condition must be inside parentheses, and the code block can be inside curly braces or a single statement without braces.Step 2: Check each option
if (condition) console.log('Yes'); correctly uses parentheses around the condition and a statement without braces.Final Answer:
if (condition) console.log('Yes'); -> Option BQuick Check:
Parentheses around condition = correct syntax [OK]
- Omitting parentheses around condition
- Using colons instead of braces
- Adding 'then' keyword (not in JS)
let x = 10;
if (x > 5) {
console.log('Big');
} else {
console.log('Small');
}Solution
Step 1: Evaluate the condition x > 5
Since x is 10, which is greater than 5, the condition is true.Step 2: Determine which block runs
The if block runs and prints 'Big'. The else block is skipped.Final Answer:
Big -> Option CQuick Check:
10 > 5 is true, so output is 'Big' [OK]
- Choosing else output when condition is true
- Confusing variable value with output
- Ignoring else block
if x == 5 {
console.log('Equal');
}Solution
Step 1: Check if statement syntax
In JavaScript, the condition must be inside parentheses after if.Step 2: Identify the error
The code misses parentheses aroundx == 5, causing a syntax error.Final Answer:
Missing parentheses around condition -> Option AQuick Check:
Condition needs parentheses in if [OK]
- Omitting parentheses around condition
- Confusing == and === (both valid but different)
- Thinking semicolon is mandatory after console.log
Solution
Step 1: Check condition for age 18 or more
The condition should be age >= 18 to include 18 and above.Step 2: Verify syntax and logic
if (age >= 18) { console.log('Adult'); } else { console.log('Minor'); } uses correct syntax and condition. if age >= 18 then { console.log('Adult'); } else { console.log('Minor'); } uses invalid 'then'. if (age > 18) { console.log('Adult'); } else { console.log('Minor'); } excludes age 18. if (age = 18) { console.log('Adult'); } else { console.log('Minor'); } uses assignment (=) instead of comparison (== or >=).Final Answer:
if (age >= 18) { console.log('Adult'); } else { console.log('Minor'); } -> Option AQuick Check:
Use >= and correct syntax for if-else [OK]
- Using assignment (=) instead of comparison
- Using 'then' keyword (not in JS)
- Using > instead of >= to include 18
Ready to prove it?
Pass the test and this pattern moves unknown
