Null and undefined types in Typescript - Time & Space Complexity
We want to understand how checking for null or undefined values affects the speed of a program.
Specifically, how does the time to run grow when we check many values for null or undefined?
Analyze the time complexity of the following code snippet.
function countValid(values: (string | null | undefined)[]): number {
let count = 0;
for (const val of values) {
if (val !== null && val !== undefined) {
count++;
}
}
return count;
}
This code counts how many items in an array are not null or undefined.
- Primary operation: Looping through each item in the array once.
- How many times: Exactly once for every item in the input array.
As the number of items grows, the time to check each item grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 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 list gets bigger.
[X] Wrong: "Checking for null or undefined is slow and adds extra loops."
[OK] Correct: The check happens inside the same loop, so it does not add extra loops or multiply the time.
Understanding how simple checks inside loops affect performance helps you write clear and efficient code.
"What if we nested another loop inside to check each character of each string? How would the time complexity change?"
Practice
null represent in TypeScript?Solution
Step 1: Understand the meaning of
nullnullmeans a variable explicitly has no value.Step 2: Compare with other options
The incorrect options describe different states or values, notnull.Final Answer:
A variable that has no value assigned -> Option DQuick Check:
nullmeans no value [OK]
- Confusing null with undefined
- Thinking null means variable is undeclared
- Assuming null means zero or empty string
null in TypeScript?Solution
Step 1: Recall union type syntax
In TypeScript, the pipe|is used to allow multiple types.Step 2: Check each option
let name: string | null; uses|correctly. The other options use invalid syntax.Final Answer:
let name: string | null; -> Option AQuick Check:
Union types use | symbol [OK]
- Using & instead of | for union types
- Writing 'or' instead of |
- Using invalid syntax like ? null
let value: number | undefined; console.log(value);
Solution
Step 1: Understand variable initialization
The variablevalueis declared but not assigned, so it defaults toundefined.Step 2: Check console.log output
Loggingvalueprintsundefinedbecause it has no assigned value.Final Answer:
undefined -> Option AQuick Check:
Unassigned variable = undefined [OK]
- Expecting null instead of undefined
- Assuming default is zero
- Thinking it causes an error
let age: number; age = null;
Solution
Step 1: Check variable type and assignment
ageis declared asnumberonly, so it cannot holdnull.Step 2: Understand TypeScript strict null checks
By default,nullis not assignable tonumberunless union type includesnull.Final Answer:
Cannot assign null to a variable of type number -> Option CQuick Check:
Assigning null to number causes error [OK]
- Ignoring TypeScript strict null checks
- Thinking null is allowed by default
- Confusing missing semicolon as error
null, or undefined?Solution
Step 1: Understand union types for parameters
To accept multiple types, use|to combine them.Step 2: Analyze each option
function greet(name: string | null | undefined) { console.log(name); } correctly usesstring | null | undefined. function greet(name: string & null & undefined) { console.log(name); } uses&which means intersection (not correct). function greet(name?: string & null) { console.log(name); } uses&which means intersection (not correct). function greet(name: string | null?) { console.log(name); } has invalid syntax.Final Answer:
function greet(name: string | null | undefined) { console.log(name); } -> Option BQuick Check:
Use | to allow multiple types in parameters [OK]
- Using & instead of | for union types
- Confusing optional parameter with undefined union
- Writing invalid syntax like null?
Ready to prove it?
Pass the test and this pattern moves unknown
