Void type for functions in Typescript - Time & Space Complexity
Let's see how the time it takes to run a function with a void return type changes as input grows.
We want to know how the function's work grows with bigger inputs.
Analyze the time complexity of the following code snippet.
function printNumbers(n: number): void {
for (let i = 0; i < n; i++) {
console.log(i);
}
}
This function prints numbers from 0 up to n-1 and returns nothing (void).
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that runs console.log for each number.
- How many times: Exactly n times, once for each number from 0 to n-1.
As n grows, the number of print actions grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 print actions |
| 100 | 100 print actions |
| 1000 | 1000 print actions |
Pattern observation: The work grows directly with n, so doubling n doubles the work.
Time Complexity: O(n)
This means the function takes longer in a straight line as the input number gets bigger.
[X] Wrong: "Since the function returns void, it must run instantly or have no cost."
[OK] Correct: The return type only says what the function gives back, not how long it takes. The loop still runs n times, so the time grows with n.
Understanding how loops affect time, even in functions that return nothing, helps you explain your code clearly and think about efficiency.
"What if we changed the loop to run only half of n times? How would the time complexity change?"
Practice
void type mean when used as a function's return type in TypeScript?Solution
Step 1: Understand the meaning of
Thevoidin TypeScriptvoidtype indicates that a function does not return any value.Step 2: Compare with other return types
Other return types likenumber,string, orobjectmean the function returns those specific types, butvoidmeans no return value.Final Answer:
The function does not return any value. -> Option CQuick Check:
voidmeans no return value [OK]
- Thinking void means returning undefined explicitly
- Confusing void with any or unknown types
- Assuming void functions return null
logMessage that returns void in TypeScript?Solution
Step 1: Check the function return type declaration
The functionlogMessageshould explicitly declare: voidto indicate no return value.Step 2: Verify the function body matches the return type
The function only logs a message and does not return anything, so: voidis correct.Final Answer:
function logMessage(): void { console.log('Hello'); } -> Option AQuick Check:
Use: voidfor no return value [OK]
- Omitting the return type annotation
- Using wrong return types like string or number
- Returning a value inside a void function
function printSum(a: number, b: number): void {
console.log(a + b);
}
const result = printSum(3, 4);
console.log(result);Solution
Step 1: Understand the function behavior
The functionprintSumlogs the sum ofaandbbut returns nothing (void).Step 2: Analyze the output of the console logs
CallingprintSum(3, 4)prints 7. The variableresultstores the return value, which isundefinedbecause the function returns void. Thenconsole.log(result)printsundefined.Final Answer:
7 and then undefined -> Option AQuick Check:
Void functions return undefined [OK]
- Assuming void functions return the logged value
- Expecting the function to return the sum
- Confusing console output with return value
function showMessage(): void {
return 'Hello';
}Solution
Step 1: Check the declared return type
The function declares its return type asvoid, meaning it should not return any value.Step 2: Analyze the return statement
The function returns the string'Hello', which conflicts with thevoidreturn type.Final Answer:
Function returns a string but declared as void -> Option DQuick Check:
Void functions must not return a value [OK]
- Returning a value inside a void function
- Ignoring TypeScript errors on return type mismatch
- Confusing void with undefined return
processData that logs a message and optionally returns a boolean indicating success. How should you declare the return type to use void correctly?Solution
Step 1: Understand the function behavior
The function sometimes returnstrue(a boolean) and sometimes only logs a message without returning anything.Step 2: Choose the correct return type
Since the function can return a boolean or nothing, the return type should be a union:boolean | void. This allows both returning a boolean and returning nothing.Final Answer:
function processData(flag: boolean): boolean | void { if(flag) return true; else console.log('No flag'); } -> Option BQuick Check:
Use union type boolean | void for optional return [OK]
- Using only void when function sometimes returns a value
- Using only boolean and missing void case
- Returning value in void-only function causing errors
Ready to prove it?
Pass the test and this pattern moves unknown
