TypeScript void Type

In TypeScript, the void type indicates that a function does not return a value.

Example

function logMessage(message: string): void {
    console.log(message);
}

logMessage("Good Morning!");  

// Output: Good Morning!

Here, the logMessage() function is declared to return void.


Using void in Function

You can use void in the function declaration as:

function functionName(): void {
    
    // Body of function

}

void With Arrow Function

The void type is also applicable to arrow functions, indicating that the function doesn't return any value. For example,

const logClick = (): void => {
    console.log("Button clicked!");
}

logClick();

Output

Button clicked!

It is particularly useful in callbacks and event handlers where you do not want to return a value accidentally.


When to Use void?

Use the void type in TypeScript when defining functions meant to perform without returning any value.

For example, if you want to display an alert message without returning any value, you can use the void type:

function showAlert(): void {
    alert("Programming Expert Found!");
}

showAlert();

Functions with a void return type typically involve side effects such as updating the UI, logging messages to the console, or triggering actions where no return value is needed or expected.

Your path to become a builder.

Builders don’t just know how to code, they create solutions that matter. Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community