If–else statement in Javascript - Time & Space Complexity
Let's see how the time it takes to run an if-else statement changes as the input changes.
We want to know how many steps the program takes when it checks conditions.
Analyze the time complexity of the following code snippet.
function checkNumber(num) {
if (num > 0) {
return 'Positive';
} else {
return 'Non-positive';
}
}
This code checks if a number is positive or not and returns a message.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single if-else condition check.
- How many times: Exactly once per function call.
Checking one condition takes the same amount of time no matter what the input number is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 check |
| 100 | 1 check |
| 1000 | 1 check |
Pattern observation: The time stays the same even if the input number changes.
Time Complexity: O(1)
This means the program takes the same short time no matter what number you give it.
[X] Wrong: "If-else takes longer when the number is bigger because it has to check more things."
[OK] Correct: The if-else only checks one condition once, so the size or value of the number does not change how long it takes.
Understanding that simple condition checks run quickly helps you explain how your code handles decisions efficiently.
"What if we added a loop that runs the if-else check multiple times? How would the time complexity change?"
Practice
if-else statement do in JavaScript?Solution
Step 1: Understand the purpose of if-else
An if-else statement lets the program decide between two paths depending on a condition.Step 2: Match the description to the options
Only It chooses between two actions based on a condition. describes choosing between two actions based on a condition.Final Answer:
It chooses between two actions based on a condition. -> Option DQuick Check:
If-else = choose between two actions [OK]
- Confusing if-else with loops
- Thinking if-else defines functions
- Believing if-else creates variables
Solution
Step 1: Recall JavaScript if-else syntax
JavaScript requires parentheses around the condition and braces for blocks: if (condition) { } else { }Step 2: Compare options to correct syntax
Only if (condition) { } else { } uses parentheses and braces correctly without extra keywords.Final Answer:
if (condition) { } else { } -> Option AQuick Check:
Correct syntax uses parentheses and braces [OK]
- Omitting parentheses around condition
- Adding 'then' keyword (not in JS)
- Missing braces for code blocks
let age = 18;
if (age >= 18) {
console.log('Adult');
} else {
console.log('Minor');
}Solution
Step 1: Evaluate the condition age >= 18
Since age is 18, the condition age >= 18 is true.Step 2: Determine which block runs
The if block runs and prints 'Adult'. The else block is skipped.Final Answer:
Adult -> Option AQuick Check:
Condition true -> if block runs -> 'Adult' [OK]
- Thinking 18 is less than 18
- Confusing else block runs when condition is true
- Expecting syntax error
let score = 75;
if score >= 60 {
console.log('Pass');
} else {
console.log('Fail');
}Solution
Step 1: Check if-else syntax
JavaScript requires parentheses around the condition in if statements.Step 2: Identify the missing parentheses
The code has if score >= 60 { ... } but it should be if (score >= 60) { ... }Final Answer:
Missing parentheses around the condition -> Option CQuick Check:
if condition must be in ( ) [OK]
- Forgetting parentheses around condition
- Thinking semicolon is required after console.log block
- Confusing else usage
let num = 7; // Choose the correct code snippet
Solution
Step 1: Understand how to check even numbers
A number is even if remainder when divided by 2 is zero: num % 2 === 0.Step 2: Check each option's condition
if (num % 2 === 0) { console.log('Even'); } else { console.log('Odd'); } correctly checks num % 2 === 0. Others either use wrong operator or logic.Final Answer:
if (num % 2 === 0) { console.log('Even'); } else { console.log('Odd'); } -> Option BQuick Check:
Even check uses num % 2 === 0 [OK]
- Using division (/) instead of modulo (%)
- Checking if remainder is truthy instead of zero
- Reversing even and odd conditions
Ready to prove it?
Pass the test and this pattern moves unknown
