Void functions in TypeScript are functions that do not return a value. They perform actions or computations without producing a result that needs to be captured. Commonly used for side effects like logging, modifying external state, or triggering asynchronous operations, they enhance code clarity.
Syntax:
function functionName(parameters: ParameterType): void {
// Function body
// No return statement or return type annotation is needed
}
Where:
- functionName is the name of the function.
- parameters are optional parameters that the function may accept.
- void is the return type annotation that indicates that the function doesn't return a value.
Examples of Void Functions in TypeScript
Example 1: Simple Void Function
In this example, the greet function is a void function. It takes one parameter, name, which is a string. The function logs a greeting message to the console and does not return a value.
JavaScript
function greet(name: string): void {
console.log(`Hello, ${name}!`);
}
greet("GeeksforGeeks");
Output:
Hello, GeeksforGeeks!
Example 2: Void Function with a Loop
In this example, the logEvenNumbers function is a void function that takes one parameter, max, which is expected to be a number. Inside the function, it iterates through numbers from 0 to max and logs each even number to the console. Since it's a void function, it doesn't return a value.
JavaScript
function logEvenNumbers(max: number): void {
for (let i = 0; i <= max; i++) {
if (i % 2 === 0) {
console.log(i);
}
}
}
logEvenNumbers(10);
Output:
0
2
4
6
8
10
Conclusion
In this article, we discussed what a void function is and how to use it in TypeScript. Essentially, a void function indicates that the function performs some actions or computations but doesn't produce a result that needs to be used or captured. Void functions are often used for operations that have side effects, such as logging to the console, modifying external state, or triggering asynchronous operations.
Similar Reads
TypeScript unknown Function In TypeScript, the unknown type is used for variables whose types aren't known in advance. It ensures type safety by requiring explicit type checks or assertions before usage, preventing arbitrary operations, and promoting safer handling compared to the `any` type.Syntaxfunction gfg(input: unknown):
3 min read
TypeScript Functions Type TypeScript function types define the structure of a function, including its parameter types and return type, ensuring consistent and type-safe usage.Help validate the types of parameters passed to a function.Ensure the function returns the expected type.Improve code clarity and prevent runtime error
6 min read
TypeScript Return type void The void type in TypeScript represents the absence of a return value for functions. It indicates that a function does not return any value, ensuring that the function's purpose is solely to act without returning data.Syntaxfunction functionName(parameters: ParameterType): void { // Function body //
2 min read
TypeScript Generic Functions TypeScript generic functions allow you to create functions that work with various types while maintaining type safety. By using type parameters, defined within angle brackets (<T>), generics enable functions to operate on different data types without losing the benefits of TypeScript's type-ch
3 min read
Void Function in MATLAB When defining void* output in the library definition file, MATLAB specifies that the argument MLTYPE must be one of these: a typedef from the library. Use only to produce scalar data. If the library has a typedef defined for void* that uses that name, MATLAB specifies MLTYPE as the new type name in
2 min read