Search⌘ K
AI Features

Returning nothing with Void

Explore the void keyword in TypeScript to understand how it marks functions that return no value. Learn why explicitly setting a function's return type to void helps avoid errors by preventing unintended return values across multiple return statements.

We'll cover the following...

Void means nothing. However, undefined can be assigned to void. The operation of setting undefined to void is not useful per se. However, a function that returns nothing should be marked with the reserved void keyword.

TypeScript 3.3.4
function executeFunctionWithoutReturnType(): void {
return undefined;
}
let returnType = executeFunctionWithoutReturnType(); // Hover the variable to see "void"
console.log(returnType);

If a function is not explicitly marked with void ...