Bird
Raised Fist0
Typescriptprogramming~10 mins

Void type for functions in Typescript - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Void type for functions
Function declared with void return type
↓
Function called
↓
Function runs code inside
↓
No value returned
↓
Execution continues without return value
This flow shows how a function with void return type runs code but does not return any value.
Execution Sample
Typescript
function greet(): void {
  console.log("Hello!");
}
greet();
This code defines a function that prints a message and returns nothing (void).
Execution Table
StepActionEvaluationResult
1Function greet declared with void return typeNo return value expectedFunction ready to be called
2greet() calledFunction starts runningInside greet function
3console.log("Hello!") executedPrints messageOutput: Hello!
4Function ends without returnNo value returnedReturn value is void (undefined)
5Execution continues after greet()No value to useProgram continues normally
💡 Function ends without returning a value because its return type is void
Variable Tracker
VariableStartAfter greet() callFinal
greetfunction declaredfunction runningfunction ended (void return)
Key Moments - 2 Insights
Why does the function not return any value even though it runs code?
Because the function's return type is void, it means it does not return any value. The execution_table row 4 shows the function ends without returning.
Can we assign the result of a void function to a variable?
No, because the function returns void (undefined), assigning its result to a variable will give undefined. See execution_table row 4 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is printed to the console?
A"Hello!"
Bundefined
Cvoid
DNo output
💡 Hint
Check the 'Result' column at step 3 in execution_table.
At which step does the function end without returning a value?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look for the step mentioning 'Function ends without return' in execution_table.
If we tried to assign greet() to a variable, what would the variable hold?
AThe string "Hello!"
Bvoid (no value)
Cundefined
DAn error
💡 Hint
Refer to variable_tracker and execution_table step 4 about return value.
Concept Snapshot
Void type for functions in TypeScript:
- Declared as function name(): void
- Means function returns no value
- Function can run code but no return statement needed
- Calling such function returns undefined
- Useful for functions that only cause side effects
Full Transcript
This visual execution trace shows how a TypeScript function with void return type works. The function greet is declared with void, meaning it does not return any value. When greet() is called, it runs the code inside, printing "Hello!" to the console. The function ends without returning anything, so its return value is void (undefined). Execution continues normally after the function call. Variables track the function state from declaration to end. Common confusions include why no value is returned and what happens if you assign the function call result to a variable. The quizzes test understanding of output, function end step, and return value assignment.

Practice

(1/5)
1. What does the void type mean when used as a function's return type in TypeScript?
easy
A. The function returns a number.
B. The function returns a string.
C. The function does not return any value.
D. The function returns an object.

Solution

  1. Step 1: Understand the meaning of void in TypeScript

    The void type indicates that a function does not return any value.
  2. Step 2: Compare with other return types

    Other return types like number, string, or object mean the function returns those specific types, but void means no return value.
  3. Final Answer:

    The function does not return any value. -> Option C
  4. Quick Check:

    void means no return value [OK]
Hint: Void means no return value from the function [OK]
Common Mistakes:
  • Thinking void means returning undefined explicitly
  • Confusing void with any or unknown types
  • Assuming void functions return null
2. Which of the following is the correct syntax to declare a function named logMessage that returns void in TypeScript?
easy
A. function logMessage(): void { console.log('Hello'); }
B. function logMessage(): string { console.log('Hello'); }
C. function logMessage(): number { console.log('Hello'); }
D. function logMessage() { return 'Hello'; }

Solution

  1. Step 1: Check the function return type declaration

    The function logMessage should explicitly declare : void to indicate no return value.
  2. Step 2: Verify the function body matches the return type

    The function only logs a message and does not return anything, so : void is correct.
  3. Final Answer:

    function logMessage(): void { console.log('Hello'); } -> Option A
  4. Quick Check:

    Use : void for no return value [OK]
Hint: Void functions have : void after parentheses [OK]
Common Mistakes:
  • Omitting the return type annotation
  • Using wrong return types like string or number
  • Returning a value inside a void function
3. What will be the output of this TypeScript code?
function printSum(a: number, b: number): void {
  console.log(a + b);
}

const result = printSum(3, 4);
console.log(result);
medium
A. 7 and then undefined
B. 7 and then 7
C. undefined and then 7
D. undefined and then undefined

Solution

  1. Step 1: Understand the function behavior

    The function printSum logs the sum of a and b but returns nothing (void).
  2. Step 2: Analyze the output of the console logs

    Calling printSum(3, 4) prints 7. The variable result stores the return value, which is undefined because the function returns void. Then console.log(result) prints undefined.
  3. Final Answer:

    7 and then undefined -> Option A
  4. Quick Check:

    Void functions return undefined [OK]
Hint: Void functions return undefined, not a value [OK]
Common Mistakes:
  • Assuming void functions return the logged value
  • Expecting the function to return the sum
  • Confusing console output with return value
4. Identify the error in this TypeScript function declaration:
function showMessage(): void {
  return 'Hello';
}
medium
A. No error, this is valid
B. Missing return type annotation
C. Function should not have a body
D. Function returns a string but declared as void

Solution

  1. Step 1: Check the declared return type

    The function declares its return type as void, meaning it should not return any value.
  2. Step 2: Analyze the return statement

    The function returns the string 'Hello', which conflicts with the void return type.
  3. Final Answer:

    Function returns a string but declared as void -> Option D
  4. Quick Check:

    Void functions must not return a value [OK]
Hint: Void means no return; returning value causes error [OK]
Common Mistakes:
  • Returning a value inside a void function
  • Ignoring TypeScript errors on return type mismatch
  • Confusing void with undefined return
5. You want to write a function processData that logs a message and optionally returns a boolean indicating success. How should you declare the return type to use void correctly?
hard
A. function processData(flag: boolean): string | boolean { if(flag) return true; else console.log('No flag'); }
B. function processData(flag: boolean): boolean | void { if(flag) return true; else console.log('No flag'); }
C. function processData(flag: boolean): void { if(flag) return true; else console.log('No flag'); }
D. function processData(flag: boolean): boolean { if(flag) return true; else console.log('No flag'); }

Solution

  1. Step 1: Understand the function behavior

    The function sometimes returns true (a boolean) and sometimes only logs a message without returning anything.
  2. 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.
  3. Final Answer:

    function processData(flag: boolean): boolean | void { if(flag) return true; else console.log('No flag'); } -> Option B
  4. Quick Check:

    Use union type boolean | void for optional return [OK]
Hint: Use union type boolean | void for optional return values [OK]
Common Mistakes:
  • Using only void when function sometimes returns a value
  • Using only boolean and missing void case
  • Returning value in void-only function causing errors
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard