The void type shows that a function does not return any value. It helps us know when a function just does something but does not give back a result.
0
0
Void type for functions in Typescript
Introduction
When a function only prints a message and does not return anything.
When a function changes something but does not need to send back a value.
When you want to make clear that a function's job is to do an action, not to calculate.
When you want to avoid accidentally returning a value from a function.
When writing event handlers that just react to events without returning data.
Syntax
Typescript
function functionName(): void { // code here }
The void type goes after the parentheses () and a colon :.
If you forget void, TypeScript may assume the function returns any or undefined.
Examples
This function prints a message and returns nothing.
Typescript
function sayHello(): void { console.log('Hello!'); }
An arrow function that logs a number and returns nothing.
Typescript
const logNumber = (num: number): void => { console.log(num); };
A function that does nothing and returns nothing.
Typescript
function doNothing(): void { // no return statement here }
Sample Program
This program defines a function that greets a person by name and prints the greeting. It does not return any value.
Typescript
function greet(name: string): void { console.log(`Hello, ${name}!`); } greet('Alice');
OutputSuccess
Important Notes
If a function has void type, it can still return undefined explicitly or just end without a return.
Using void helps other programmers understand the function's purpose quickly.
Summary
void means a function does not return a value.
Use it to show a function only does something but does not give back data.
It helps keep code clear and avoid mistakes with unexpected return values.