Returning nothing with Void
In this lesson, you will learn how to explicitly mention something that does nothing.
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.
Press + to interact
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 as the return value, then by default, TypeScript will mark the return value as void. This may be problematic, because a programmer may return anything within the function. If it is explicitly set to void, ...