Skip to main content

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

javascript
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:
javascript
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.

javascript
function sayHello(): void { console.log("Hello!"); } const result = sayHello(); console.log(result); // undefined

void means "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.

javascript
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));
  • callback has type (item: T) => void: it is called, but its result is not used.
  • Even if something is returned inside callback, TypeScript ignores it:
javascript
forEach([1, 2, 3], (n) => n * 2); // ok, the return value is ignored

Example 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.

javascript
async function saveData(): Promise<void> { console.log("Saving data..."); }

or when calling it:

javascript
function startProcess(): void { void saveData(); // deliberately telling TypeScript: "don't wait for the result" }

Here void before the call is an operator, not a type (it discards the value). This is a common pattern in TS when calling async functions without await.


Example 5. The difference between void and undefined

TypeDescriptionExample
void"returns nothing (is not used)"function log(): void { ... }
undefined"explicitly returns the value undefined"function empty(): undefined { return undefined; }
javascript
function a(): void {} function b(): undefined { return undefined; } const x = a(); // x has type void const y = b(); // y has type undefined

void and undefined are close, but not interchangeable. void is a promise "to return nothing", while undefined is an actual value.


Example 6. Using void in interfaces

javascript
interface ButtonProps { label: string; onClick: () => void; } const button: ButtonProps = { label: "Click", onClick: () => { console.log("Button clicked!"); }, };

onClick has type () => void, because the event handler does not return a result.


Summary

PropertyValue
Where it is usedA function's return type
What it means"The function returns nothing"
Actual behavior in JSReturns undefined, but that is ignored
UsageCallbacks, event handlers, loggers, sid

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.