Lesson 10 of 60 – Comments in TypeScript
17%

Comments in TypeScript

Comments are notes written inside a program to explain what the code does. Comments are ignored by the TypeScript compiler and are not executed as program instructions.

Note: TypeScript uses the same comment syntax as JavaScript. You can use single-line comments and multi-line comments to make your code easier to understand.

1. What is a Comment?

A comment is a piece of text written inside source code for explanation or documentation.

// This is a comment

The compiler does not treat the comment as executable TypeScript code.

2. Why Use Comments?

Comments can help developers understand the purpose of code. They are especially useful when programs become larger.

Comments can be used to:

  • Explain code
  • Describe a calculation
  • Document a function
  • Temporarily disable code
  • Make complex code easier to understand

3. Single-Line Comments

A single-line comment starts with two forward slashes: //

// This is a single-line comment

let name = "Rahul";

Everything after // on that line is treated as a comment.

4. Comment Before Code

A comment can be written above a line of code.

// Store the student's name
let studentName: string = "Amit";

The comment explains what the next statement does.

5. Comment After Code

A comment can also be written after a statement.

let age: number = 20; // Student age

This is useful for short explanations.

6. Multiple Single-Line Comments

You can write multiple single-line comments.

// Student information
// Store name
// Store age

let name: string = "Rahul";
let age: number = 21;

7. Multi-Line Comments

A multi-line comment starts with /* and ends with */.

/*
This is a multi-line comment.
It can contain multiple lines.
*/

let name = "Rahul";

8. Multi-Line Comment Example

/*
Student Information Program
This program stores
student name and marks.
*/

let name: string = "Amit";
let marks: number = 85;

Multi-line comments are useful for longer explanations.

9. Comments Are Ignored by the Compiler

Comments are not executed as program instructions.

// Print a message

console.log("Hello TypeScript");

Only the console.log() statement performs an action.

10. Commenting Variables

Comments can explain the purpose of variables.

// Store student's name
let studentName: string = "Rahul";

// Store student's marks
let marks: number = 90;

11. Commenting Functions

You can write a comment to describe what a function does.

// Add two numbers
function add(
    a: number,
    b: number
): number {

    return a + b;
}

12. Commenting Function Parameters

function calculateTotal(
    price: number,
    quantity: number
): number {

    // Multiply price by quantity
    return price * quantity;
}

The comment explains the calculation performed by the function.

13. Comments for Calculations

let price: number = 500;
let quantity: number = 3;

// Calculate total price
let total: number = price * quantity;

console.log(total);

14. Comments for Conditions

let age: number = 20;

// Check whether the person is an adult
if (age >= 18) {
    console.log("Adult");
}

The comment explains the purpose of the condition.

15. Comments in Loops

let numbers: number[] = [10, 20, 30, 40];

// Print every number
for (let number of numbers) {
    console.log(number);
}

16. Temporarily Disable Code

Comments can be used to temporarily disable a line of code.

let name = "Rahul";

// console.log(name);

console.log("Program completed");

The commented console.log() statement will not execute.

17. Commenting Multiple Lines of Code

Multiple lines can be temporarily disabled using a multi-line comment.

/*
console.log("Line 1");
console.log("Line 2");
console.log("Line 3");
*/

All three statements are inside the comment.

18. Comments and Type Annotations

// Student name must be a string
let name: string = "Amit";

// Student marks must be a number
let marks: number = 85;

// Result is true or false
let passed: boolean = true;

Comments can make type annotations easier for beginners to understand.

19. Comments with Arrays

// Store student marks
let marks: number[] = [
    80,
    90,
    75,
    88
];

The comment describes the purpose of the array.

20. Comments with Objects

// Store student information
let student: {
    name: string;
    age: number;
} = {

    name: "Rahul",
    age: 21

};

21. Comments with Interfaces

// Define the structure of a student
interface Student {

    name: string;
    age: number;
    marks: number;

}

The comment describes the purpose of the interface.

22. Comments in React Code

Comments can also be used in React TypeScript projects.

// Display welcome message

function Welcome() {

    return <h1>Welcome</h1>;

}

23. Comments in React Native

Comments are also useful when developing React Native applications.

// Display the application title

<Text>
    Welcome to My App
</Text>

Comments can explain the purpose of components and application logic.

24. Good Comments vs Bad Comments

Good Comment
// Calculate the final price after quantity
let total = price * quantity;
Less Useful Comment
// Add two numbers
let a = 10;
let b = 20;

A good comment should provide useful information rather than simply repeat obvious code.

25. Avoid Unnecessary Comments

You do not need to comment every simple line of code.

let name = "Rahul";

The purpose of this statement may already be clear.

Use comments when they add useful information or explain something that may not be immediately obvious.

26. Comments for Business Logic

Comments are particularly useful for explaining business rules or non-obvious application logic.

// Apply a discount when the order amount
// is greater than or equal to 5000

if (amount >= 5000) {
    discount = 10;
}

27. Comments for React Native Application Logic

// Check whether the user is logged in
// before displaying the profile screen

if (isLoggedIn) {
    console.log("Open Profile");
}

Such comments can help developers understand why a particular piece of application logic exists.

28. Complete Comment Example

// Store student information
let studentName: string = "Amit";
let marks: number = 85;

// Calculate result
let passed: boolean = marks >= 40;

// Display student information
console.log("Name:", studentName);
console.log("Marks:", marks);
console.log("Passed:", passed);

This example uses comments to separate different parts of the program.

29. Comment Syntax Summary

Type Syntax Use
Single-line // comment Short explanation
Multi-line /* comment */ Longer explanation
Inline let age = 20; // age Short explanation after code

30. Best Practices for Comments

Follow these practices when writing comments:

  1. Write comments that explain useful information.
  2. Keep comments short and clear.
  3. Use comments for complex logic.
  4. Use comments to document important functions.
  5. Avoid comments that simply repeat obvious code.
  6. Update comments when the related code changes.
  7. Use single-line comments for short explanations.
  8. Use multi-line comments for longer explanations.
Remember: Comments help humans understand code, but they are not executed as program instructions.

📌 Key Points

  • Comments are notes written inside source code.
  • TypeScript uses the same comment syntax as JavaScript.
  • // creates a single-line comment.
  • /* */ creates a multi-line comment.
  • Comments are ignored as executable instructions.
  • Comments can explain variables, functions, conditions, and logic.
  • Comments can temporarily disable code.
  • Good comments explain useful or non-obvious information.
  • Avoid unnecessary comments that simply repeat the code.
  • Comments are useful in React and React Native projects.

🧠 Quick Quiz

Question: Which symbol is used to create a single-line comment in TypeScript?