# The void Type

> The type for functions that don't return anything.

Subject: TypeScript | Lesson 4 of 54 | 3 min read
Interactive lesson: https://codewithanimation.in/typescript/void-type
Previous: [The never Type](https://codewithanimation.in/typescript/never-type.md) | Next: [Arrays & Tuples](https://codewithanimation.in/typescript/arrays-and-tuples.md)

## The Absence of a Value

`void` is the opposite of `any`. It signifies the complete absence of a type.

You will almost exclusively see it used as the return type of functions that perform side effects (like updating the DOM or logging to the console) rather than returning a value.

## Preventing Accidents

If you declare that a function returns `void`, TypeScript will stop you from accidentally assigning its result to a variable or attempting to chain methods off of it.

If you write `const result = console.log('hello');`, `result` is typed as `void`. If you then try to do `result.toString()`, the compiler blocks you.

## Typing function returns

You specify `void` after the parameter list of a function:

```typescript
function greet(name: string): void {
  console.log(`Hello, ${name}!`);
  // No return statement here
}
```

## Common mistake: void vs undefined

A common point of confusion is the difference between `void` and `undefined`. In JavaScript, a function that doesn't return anything actually returns `undefined` at runtime.

However, in TypeScript, if you explicitly type a return as `undefined`, you *must* have a literal `return undefined;` or `return;` statement. `void` is much more relaxed: it just means 'I do not care what this returns'.

## Tip: The callback exception

There is one special quirk with `void`. If you define a callback type like `type Logger = () => void`, TypeScript will actually allow you to pass a function that returns a value (like `() => true`).

Why? Because `void` in this specific context means 'the caller of this callback will safely ignore any returned value'. This matches how native JS arrays like `.forEach` work.

## Try it: Try it

Watch how TypeScript handles a function typed with a `void` return. It enforces that you cannot use the resulting value.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/void-type

## Check yourself

### Question 1

What is the primary use case for the 'void' type in TypeScript?

- A. To represent an uninitialized variable.
- B. As the return type for functions that do not return a value.
- C. To explicitly ban 'null' values.

Correct answer: B

'void' is used to type functions that exist solely for side effects and return nothing.

- Why not A: That is represented by 'undefined'.
- Why not C: 'strictNullChecks' handles banning nulls, not the 'void' type.

## Remember this

- `void` represents the absence of a returned value.
- It prevents developers from accidentally using the result of a side-effect function.
- Unlike `undefined`, `void` does not force you to write a `return` statement.
