Challenge - 5 Problems
Void Type Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a void function with console.log
What is the output of the following TypeScript code?
Typescript
function greet(): void { console.log('Hello!'); } const result = greet(); console.log(result);
Attempts:
2 left
💡 Hint
Remember that functions with void return type do not return a value, so the result is undefined.
✗ Incorrect
The function greet prints 'Hello!' to the console. Since it returns void, the variable result is undefined. So the console.log(result) prints 'undefined'.
🧠 Conceptual
intermediate1:30remaining
Understanding void return type in TypeScript
Which statement about the void return type in TypeScript functions is correct?
Attempts:
2 left
💡 Hint
Think about what happens when you omit a return statement in a void function.
✗ Incorrect
In TypeScript, a function declared with void return type can either return nothing or return undefined explicitly. Returning other values causes an error.
🔧 Debug
advanced1:30remaining
Identify the error with void function return
What error will this TypeScript code produce?
Typescript
function calculate(): void { return 5; }
Attempts:
2 left
💡 Hint
Check the return type and what is being returned.
✗ Incorrect
A function declared with void return type cannot return a value other than undefined. Returning 5 causes a type error.
❓ Predict Output
advanced2:00remaining
Void function with callback parameter
What will be the output of this TypeScript code?
Typescript
function process(callback: () => void): void { console.log('Start'); callback(); console.log('End'); } process(() => console.log('Inside callback'));
Attempts:
2 left
💡 Hint
The callback is called between the two console.log statements.
✗ Incorrect
The function prints 'Start', then calls the callback which prints 'Inside callback', then prints 'End'.
🧠 Conceptual
expert2:00remaining
Void type and async functions in TypeScript
Which statement about async functions with void return type in TypeScript is true?
Attempts:
2 left
💡 Hint
Think about what async functions return in TypeScript.
✗ Incorrect
Async functions always return a Promise. Declaring return type void means the Promise resolves with no value, so the actual return type is Promise.