Void and undefined

Similarity


function testFn(): void {
 
}

function testFn2(): undefined {
 
}

console.log(testFn()); // undefined
  • Both cases are accepted, since if no return value is specified , javascript will return undefined

Difference

  • A contextual function type with a void return type (type voidFunc = () => void), when implemented, can return any other value, but it will be ignored.

  • When a literal function definition has a void return type, that function must not return anything.

Last updated

Was this helpful?