TypeScript Comments

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

  1. Definition
  2. Syntax
  3. Types of Comments
    • Single-Line Comments
    • Multi-Line Comments
  4. Examples and Output
  5. 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.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare