0
0
Typescriptprogramming~20 mins

Void type for functions in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Void Type Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
Aundefined\nHello!
BHello!\nvoid
CHello!\nundefined
DHello!
Attempts:
2 left
💡 Hint
Remember that functions with void return type do not return a value, so the result is undefined.
🧠 Conceptual
intermediate
1:30remaining
Understanding void return type in TypeScript
Which statement about the void return type in TypeScript functions is correct?
AA function with void return type can return undefined or no value at all.
BA function with void return type returns the string 'void' by default.
CA function with void return type must always return null explicitly.
DA function with void return type cannot return any value, not even undefined.
Attempts:
2 left
💡 Hint
Think about what happens when you omit a return statement in a void function.
🔧 Debug
advanced
1:30remaining
Identify the error with void function return
What error will this TypeScript code produce?
Typescript
function calculate(): void {
  return 5;
}
AType 'number' is not assignable to type 'void'.
BNo error, function returns 5 correctly.
CSyntaxError: Unexpected return value.
DRuntime error: Cannot return value from void function.
Attempts:
2 left
💡 Hint
Check the return type and what is being returned.
Predict Output
advanced
2: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'));
AInside callback\nStart\nEnd
BStart\nInside callback\nEnd
CStart\nEnd\nInside callback
DStart\nEnd
Attempts:
2 left
💡 Hint
The callback is called between the two console.log statements.
🧠 Conceptual
expert
2:00remaining
Void type and async functions in TypeScript
Which statement about async functions with void return type in TypeScript is true?
ADeclaring async functions with void return type causes a runtime error.
BAsync functions cannot have void return type because they always return a Promise.
CAsync functions with void return type return undefined immediately.
DAn async function declared with void return type actually returns Promise<void>.
Attempts:
2 left
💡 Hint
Think about what async functions return in TypeScript.