Interview Q&A / TypeScript

BeginnerQuick answer1 min readUpdated 3 Oct 2026

What does the void type mean, and how is it different from undefined?

SHORT ANSWER

void marks a function whose return value is meant to be ignored. undefined is an actual value. A function typed to return void may still be assigned a function that returns something, but its result cannot be used.

Explain it simply

A peon who delivers a letter does not hand anything back. You do not ask what he returned. That is void: the caller promises not to care.

undefined is different: it is a real value you can store, compare and return. A function that must return exactly undefined has to say so with a return statement.

The odd part is callbacks. If a parameter is typed () => void, TypeScript accepts a callback that returns a number too, because the result will be thrown away. This is why forEach accepts an arrow function that happens to return something.

Example

ts
function log(msg: string): void {
  console.log(msg);            // no return needed
}

type Callback = () => void;
const cb: Callback = () => 42; // allowed: result ignored
// const x: number = cb();     // Error: void is not assignable to number

function explicit(): undefined {
  return undefined;            // must return, even if just undefined
}

[1, 2, 3].forEach(n => n * 2); // callback returns number, forEach expects void

What to say in the interview

"void means the return value should not be used; undefined is a concrete value. A function declared as returning void can still be satisfied by one that returns something, which is why callbacks like forEach are flexible. If I need to return exactly undefined I annotate undefined and write the return."

Common mistakes

  • Using void as a variable type, where it is nearly useless.

#basic-types#functions

Related questions

Practise more TypeScript questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open TypeScript quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.