🎓 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 learn about comments in TypeScript. Comments are essential for making your code more readable and maintainable. They allow you to include explanations, notes, or reminders within your code without affecting its execution.
Table of Contents
- Definition
- Syntax
- Types of Comments
- Single-Line Comments
- Multi-Line Comments
- Examples and Output
- Conclusion
Definition
Comments in TypeScript are text annotations that you can include in your code to explain, document, or temporarily disable certain parts of the code. Comments are ignored by the TypeScript compiler and do not affect the execution of the program.
Syntax
TypeScript follows the same syntax as JavaScript for comments.
Basic Syntax
// Single-line comment
/* Multi-line comment */
Types of Comments
Single-Line Comments
Definition
Single-line comments are used for short explanations or notes. They start with // and extend to the end of the line.
Syntax
// This is a single-line comment
Example
This example shows how to use a single-line comment to explain a line of code.
let age: number = 25; // Declare a variable 'age' and assign the value 25
console.log(age);
Output
25
Multi-Line Comments
Definition
Multi-line comments are used for longer explanations or to comment out blocks of code. They start with /* and end with */.
Syntax
/* This is a multi-line comment
It can span multiple lines */
Example
This example shows how to use a multi-line comment to explain a block of code.
/* Declare a constant 'pi'
and assign the value 3.14 */
const pi: number = 3.14;
console.log(pi);
Output
3.14
Examples and Output
Example 1: Single-Line Comment
In this example, we use a single-line comment to describe the purpose of the variable.
let userName: string = "Ravi"; // This variable stores the user's name
console.log(userName); // Output: Ravi
Example 2: Multi-Line Comment
In this example, we use a multi-line comment to describe the purpose of a function.
/* This function greets the user
with a welcome message */
function greet(name: string): string {
return `Hello, ${name}! Welcome to TypeScript.`;
}
console.log(greet("Ravi")); // Output: Hello, Ravi! Welcome to TypeScript.
Complete Example
Let's put all the examples together in one file.
TypeScript Code (src/index.ts)
// Single-line comment example
let userName: string = "Ravi"; // This variable stores the user's name
console.log(userName); // Output: Ravi
/* Multi-line comment example */
/* This function greets the user
with a welcome message */
function greet(name: string): string {
return `Hello, ${name}! Welcome to TypeScript.`;
}
console.log(greet("Ravi")); // Output: Hello, Ravi! Welcome to TypeScript.
Compiling to JavaScript
To compile the TypeScript code to JavaScript, run the TypeScript compiler:
tsc src/index.ts
Output in JavaScript (src/index.js)
// Single-line comment example
var userName = "Ravi"; // This variable stores the user's name
console.log(userName); // Output: Ravi
/* Multi-line comment example */
/* This function greets the user
with a welcome message */
function greet(name) {
return "Hello, " + name + "! Welcome to TypeScript.";
}
console.log(greet("Ravi")); // Output: Hello, Ravi! Welcome to TypeScript.
Running the JavaScript
To see the output of the compiled JavaScript code, run the JavaScript file using Node.js:
node src/index.js
Conclusion
In this chapter, we learned about comments in TypeScript. We covered the syntax for single-line and multi-line comments and provided examples of each. Comments are essential for making your code more readable and maintainable.
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