Boolean type behavior in Typescript - Time & Space Complexity
We want to understand how the time it takes to work with Boolean values changes as we use more of them.
How does the program's work grow when handling many Boolean values?
Analyze the time complexity of the following code snippet.
function countTrue(values: boolean[]): number {
let count = 0;
for (const val of values) {
if (val) {
count++;
}
}
return count;
}
This code counts how many true values are in a list of Booleans.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each Boolean in the array once.
- How many times: Exactly once for each item in the input list.
As the list gets longer, the program checks each Boolean one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The work grows directly with the number of Booleans.
Time Complexity: O(n)
This means the time to count true values grows in a straight line as the list gets longer.
[X] Wrong: "Checking Booleans is instant and does not depend on list size."
[OK] Correct: Even though each check is quick, you must check every item, so more items mean more total work.
Understanding how simple loops over Booleans scale helps you explain basic efficiency clearly and confidently.
"What if we changed the array to a nested array of Booleans? How would the time complexity change?"
Practice
Solution
Step 1: Understand TypeScript boolean type
TypeScript uses the lowercasebooleantype to declare true/false values.Step 2: Check each option's syntax
let isDone: boolean = true; usesbooleanwith a true value correctly. let isDone: bool = true; usesboolwhich is invalid. let isDone = Boolean; assigns the Boolean constructor, not a value. let isDone: Boolean = 'true'; uses uppercaseBooleanand a string, which is incorrect.Final Answer:
let isDone: boolean = true; -> Option DQuick Check:
TypeScript boolean type = let var: boolean = true [OK]
- Using 'bool' instead of 'boolean'
- Assigning string 'true' instead of boolean true
- Using uppercase 'Boolean' type incorrectly
Solution
Step 1: Identify valid boolean values
Boolean values in TypeScript are onlytrueorfalsewithout quotes and lowercase.Step 2: Evaluate each option
let flag: boolean = false; correctly assignsfalse. let flag: boolean = 'false'; uses a string 'false' which is invalid. let flag: boolean = 0; uses number 0 which is not boolean. let flag: boolean = True; uses capitalized True which is invalid in TypeScript.Final Answer:
let flag: boolean = false; -> Option AQuick Check:
Boolean values are true or false without quotes [OK]
- Using strings 'true' or 'false' instead of boolean literals
- Using numbers like 0 or 1 as booleans
- Capitalizing true or false
let a: boolean = true;
let b: boolean = false;
console.log(a && b);
Solution
Step 1: Understand boolean AND operation
The&&operator returns true only if both operands are true.Step 2: Evaluate the operands
Variableais true,bis false, sotrue && falseis false.Final Answer:
false -> Option BQuick Check:
true AND false = false [OK]
- Assuming true && false is true
- Confusing && with || operator
- Expecting runtime error for boolean operations
let isActive: boolean = True;
console.log(isActive);
Solution
Step 1: Check boolean literal case sensitivity
In TypeScript, boolean literals are lowercasetrueandfalse.Truewith uppercase T is undefined.Step 2: Understand error type
UsingTruecauses a ReferenceError because it is not declared or recognized as a boolean.Final Answer:
SyntaxError: True is not defined -> Option CQuick Check:
Boolean literals must be lowercase true or false [OK]
- Using uppercase True or False
- Expecting TypeScript to auto-correct case
- Confusing boolean literals with strings
function checkValue(val: any): boolean {
return !!val;
}
console.log(checkValue(0));
console.log(checkValue('hello'));
console.log(checkValue(null));What will be the output of this code?
Solution
Step 1: Understand double negation (!!) in TypeScript
The!!converts any value to its boolean equivalent: falsy values become false, truthy become true.Step 2: Evaluate each input
0 is falsy, so !!0 is false. 'hello' is a non-empty string (truthy), so !!'hello' is true. null is falsy, so !!null is false.Final Answer:
false true false -> Option AQuick Check:
!!value converts to boolean true/false correctly [OK]
- Assuming 0 is true
- Confusing !! with single !
- Expecting null to be true
Ready to prove it?
Pass the test and this pattern moves unknown
