Logical operators in Javascript - Time & Space Complexity
Logical operators combine true or false values to make decisions in code.
We want to see how the time to run code changes when using these operators.
Analyze the time complexity of the following code snippet.
function checkValues(arr) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] && arr[i].isActive) {
console.log('Active item found');
}
}
}
This code checks each item in an array to see if it exists and is active.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each element of the array.
- How many times: Once for every item in the array.
As the array gets bigger, the code checks more items one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of checks grows directly with the number of items.
Time Complexity: O(n)
This means the time to run grows in a straight line as the input size grows.
[X] Wrong: "Logical operators make the code run faster because they skip some checks."
[OK] Correct: Logical operators do short-circuit, but the loop still runs once per item, so time grows with input size.
Understanding how logical operators affect loops helps you explain code efficiency clearly and confidently.
"What if we replaced the loop with a recursive function? How would the time complexity change?"
Practice
true only if both conditions are true?Solution
Step 1: Understand AND operator behavior
The AND operator (&&) returns true only when both conditions it connects are true.Step 2: Compare with other operators
The OR operator (||) returns true if at least one condition is true, NOT (!) flips true/false, and ? is a ternary operator, not a logical operator.Final Answer:
&& (AND) -> Option CQuick Check:
AND = && [OK]
- Confusing OR (||) with AND (&&)
- Thinking NOT (!) combines conditions
- Mixing ternary operator with logical operators
isActive in JavaScript?Solution
Step 1: Recall NOT operator syntax
In JavaScript, the NOT operator is a single exclamation mark (!) placed before the variable to flip its boolean value.Step 2: Check each option
!isActive is the correct syntax. Options A, C, and D are invalid syntax in JavaScript.Final Answer:
!isActive -> Option BQuick Check:
NOT = ! before variable [OK]
- Writing not instead of !
- Placing ! after variable
- Using method calls like .not()
const a = true;
const b = false;
console.log(a || b && !b);
Solution
Step 1: Evaluate NOT operator first
!b means !false which is true.Step 2: Evaluate AND operator next
b && !b is false && true which is false.Step 3: Evaluate OR operator last
a || (result of AND) is true || false which is true.Final Answer:
true -> Option AQuick Check:
Operator precedence: ! > && > || = true [OK]
- Ignoring operator precedence
- Evaluating OR before AND
- Misunderstanding ! operator
let x = true;
if (x && !x) {
console.log('Yes');
} else {
console.log('No');
}
Solution
Step 1: Analyze the condition x && !x
x is true, !x is false, so true && false is always false.Step 2: Understand effect on code flow
Since condition is always false, 'Yes' will never print, only 'No' will print.Final Answer:
The condition x && !x is always false -> Option AQuick Check:
Contradictory condition = always false [OK]
- Thinking !x is invalid on boolean
- Expecting 'Yes' to print
- Confusing syntax errors with logic errors
const loggedIn = true;
const isAdmin = false;
// Choose the correct condition to show admin panel
Solution
Step 1: Understand requirement
Both loggedIn and isAdmin must be true to show admin panel.Step 2: Check each condition
The condition with || (OR) is true if at least one is true. The condition with !loggedIn || !isAdmin is true unless both are true. The condition with !loggedIn && isAdmin is true only if not logged in and is admin. The condition with && (AND) requires both true.Final Answer:
if (loggedIn && isAdmin) { showAdminPanel(); } -> Option DQuick Check:
Both true needed = use AND (&&) [OK]
- Using OR (||) when both conditions are required
- Negating conditions incorrectly
- Confusing logic for access control
Ready to prove it?
Pass the test and this pattern moves unknown
