Q39. What is the difference between void and undefined?
void is commonly used as a function return annotation when the caller should ignore the return value. undefined is a concrete JavaScript value and a type. A function returning void can sometimes be implemented by a function returning a value, because callers promise not to use that value.
Use void for callback contracts where the result is intentionally ignored; use undefined when the actual value matters to the contract.
Note
Interview approach: define the concept, explain why it matters, then mention one trade-off or limitation.
TypeScript concept
// Illustrative TypeScript contract
type Identifier = string;
const id: Identifier = "item-1";💡 The example demonstrates the compile-time contract discussed in this question.
Key Takeaways
- Explain the concept in your own words.
- Know the practical trade-off and its limits.
- Use a small example to demonstrate the idea.
Knowledge Check
Which is the usual return annotation for a side-effect callback?