🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
In this chapter, we will explore the void type in TypeScript. The void type is typically used to indicate that a function does not return a value. Understanding how to use the void type is essential for correctly defining function return types in your TypeScript programs.
Table of Contents
- Definition
- Using the
voidType - Functions with
voidReturn Type - Variables of Type
void - Complete Example with Output
- Conclusion
Definition
The void type in TypeScript is used to represent the absence of a value. It is most commonly used as the return type of functions that do not return a value. When a function is specified to return void, it means the function doesn't return any value.
Using the void Type
The void type is primarily used to indicate that a function does not return a value. It can also be used with variables, though this is less common and not very useful in most cases.
Functions with void Return Type
When a function does not return a value, you should specify the return type as void. This makes it clear that the function is intended to perform some actions without producing a result.
Example
function logMessage(message: string): void {
console.log(message);
}
logMessage("Hello, TypeScript!"); // Output: Hello, TypeScript!
Output
Hello, TypeScript!
Variables of Type void
Declaring variables of type void is not very useful and is generally discouraged, as void means the absence of any value. However, it can be done for specific use cases, such as function parameters that should not be used.
Example
let unusable: void;
unusable = undefined; // The only values you can assign to a variable of type `void` are `undefined` or `null` (if `--strictNullChecks` is not enabled)
console.log(unusable); // Output: undefined
Output
undefined
Complete Example with Output
In this section, we will combine all the examples into a single TypeScript file, compile it to JavaScript, and run it to see the output.
TypeScript Code
You can test the following code in the TypeScript Playground:
// Functions with `void` Return Type
function logMessage(message: string): void {
console.log(message);
}
logMessage("Hello, TypeScript!"); // Output: Hello, TypeScript!
// Variables of Type `void`
let unusable: void;
unusable = undefined; // The only values you can assign to a variable of type `void` are `undefined` or `null` (if `--strictNullChecks` is not enabled)
console.log(unusable); // Output: undefined
Conclusion
In this chapter, we covered the void type in TypeScript, including how to use it to indicate that a function does not return a value, and how to declare variables of type void. We provided a complete example with its output to illustrate how the void type works in TypeScript. Understanding the void type is essential for correctly defining function return types in your TypeScript programs.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment