![]() |
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
// No return statement or return type annotation is needed
} Example 1: Void Function with String ParameterIn this example, greet is a void function that takes one parameter name, which is expected to be a string. The function logs a greeting message to the console. Since it’s a void function, it does not return a value.
Output: Hello, Geeks! Example 2: Void Function with Number ParameterIn this example, logEvenNumbers 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 does not return a value.
Output: 0 FAQs – TypeScript Return type voidCan a void function return null or undefined?
What happens if a void function accidentally returns a value?
Can you assign a void function to a variable?
How is void different from undefined in TypeScript?
Can you use void for other types besides functions?
|
Reffered: https://www.geeksforgeeks.org
Geeks Premier League |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |