Suggest an editImprove this articleRefine the answer for “void in a function”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)The `void` type on a function means that **this function returns nothing (the result is not used)**: the function performs some action but does not return a value via `return`. **Key point:** a function without a `return` actually always returns `undefined`, but from TS's type perspective this is `void`, meaning "returns nothing meaningful", not "returns literally nothing".Shown above the full answer for quick recall.Answer (EN)Image## 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` | Type | Description | Example | |---|---|---| | `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 | 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, sidFor the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.