Horje
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.

Syntax

function functionName(parameters: ParameterType): void {
    // Function body
    // No return statement or return type annotation is needed
}

Example 1: Void Function with String Parameter

In 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.

TypeScript
function greet(name: string): void {
    console.log(`Hello, ${name}!`);
}

greet("Geeks");

Output:

Hello, Geeks!

Example 2: Void Function with Number Parameter

In 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.

TypeScript
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

FAQs – TypeScript Return type void

Can a void function return null or undefined?

No, a function with a void return type cannot return any value, including null or undefined. It should not have a return statement that returns a value.

What happens if a void function accidentally returns a value?

If a void function returns a value, TypeScript will generate a compile-time error indicating that a function returning void cannot return a value.

Can you assign a void function to a variable?

Yes, you can assign a void function to a variable, but calling that variable as a function will not produce a return value.

let func = (): void => {
console.log("This is a void function");
};
func(); // Outputs: "This is a void function"

How is void different from undefined in TypeScript?

void is used as a return type for functions that do not return a value, while undefined is a type that represents an undefined value. void indicates the absence of any value being returned.

Can you use void for other types besides functions?

While void is primarily used for function return types, it can technically be used in other contexts, but it is not common practice. Its primary use is to indicate that a function does not return a value.




Reffered: https://www.geeksforgeeks.org


Geeks Premier League

Related
TypeScript Assertion functions TypeScript Assertion functions
How to send push notification using XMPP Server? How to send push notification using XMPP Server?
Vue.js Prop Passing Details Vue.js Prop Passing Details
Cloudflare Load Balancer Pricing | Analyzing the Cost and Benefits Cloudflare Load Balancer Pricing | Analyzing the Cost and Benefits
How to Fix the Calculator App Not Working in Windows? How to Fix the Calculator App Not Working in Windows?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
11