void in a function
What void means in a function
The void type on a function means:
"This function returns nothing (the result is not used)".
That is, the function performs some action (logs, changes state, triggers a side effect),
but does not return a value via return.
Example 1. A simple function with void
function logMessage(message: string): void {
console.log("Message:", message);
}- The function returns nothing, it only prints to the console.
- If you try to return something, TypeScript shows an error:
function logMessage(message: string): void {
return "Done"; // Error: a function with type void must not return a value
}Example 2. Actually undefined is returned, but the type is void
Functions in JavaScript that have no return
always return undefined, but from TS's type perspective this is void.
function sayHello(): void {
console.log("Hello!");
}
const result = sayHello();
console.log(result); // undefined
voidmeans "returns nothing meaningful", not "returns literally nothing".
Example 3. Callback functions with void
The void type is often used in callback functions,
where the result of the call does not matter.
function forEach<T>(array: T[], callback: (item: T) => void): void {
for (const item of array) {
callback(item);
}
}
forEach([1, 2, 3], (n) => console.log(n * 2));callbackhas type(item: T) => void: it is called, but its result is not used.- Even if something is returned inside
callback, TypeScript ignores it:
forEach([1, 2, 3], (n) => n * 2); // ok, the return value is ignoredExample 4. An async function with void
Sometimes void is used to explicitly show:
the result of the promise is not used, and we do not wait for it to finish.
async function saveData(): Promise<void> {
console.log("Saving data...");
}or when calling it:
function startProcess(): void {
void saveData(); // deliberately telling TypeScript: "don't wait for the result"
}Here
voidbefore the call is an operator, not a type (it discards the value). This is a common pattern in TS when calling async functions withoutawait.
Example 5. The difference between void and undefined
| Type | Description | Example |
|---|---|---|
void | "returns nothing (is not used)" | function log(): void { ... } |
undefined | "explicitly returns the value undefined" | function empty(): undefined { return undefined; } |
function a(): void {}
function b(): undefined { return undefined; }
const x = a(); // x has type void
const y = b(); // y has type undefined
voidandundefinedare close, but not interchangeable.voidis a promise "to return nothing", whileundefinedis an actual value.
Example 6. Using void in interfaces
interface ButtonProps {
label: string;
onClick: () => void;
}
const button: ButtonProps = {
label: "Click",
onClick: () => {
console.log("Button clicked!");
},
};
onClickhas type() => void, because the event handler does not return a result.
Summary
| Property | Value |
|---|---|
| Where it is used | A function's return type |
| What it means | "The function returns nothing" |
| Actual behavior in JS | Returns undefined, but that is ignored |
| Usage | Callbacks, event handlers, loggers, sid |
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.