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.
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.
Comments can help developers understand the purpose of code. They are especially useful when programs become larger.
Comments can be used to:
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.
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.
A comment can also be written after a statement.
let age: number = 20; // Student age
This is useful for short explanations.
You can write multiple single-line comments.
// Student information
// Store name
// Store age
let name: string = "Rahul";
let age: number = 21;
A multi-line comment starts with /* and ends with
*/.
/*
This is a multi-line comment.
It can contain multiple lines.
*/
let name = "Rahul";
/*
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.
Comments are not executed as program instructions.
// Print a message
console.log("Hello TypeScript");
Only the console.log() statement performs an action.
Comments can explain the purpose of variables.
// Store student's name
let studentName: string = "Rahul";
// Store student's marks
let marks: number = 90;
You can write a comment to describe what a function does.
// Add two numbers
function add(
a: number,
b: number
): number {
return a + b;
}
function calculateTotal(
price: number,
quantity: number
): number {
// Multiply price by quantity
return price * quantity;
}
The comment explains the calculation performed by the function.
let price: number = 500;
let quantity: number = 3;
// Calculate total price
let total: number = price * quantity;
console.log(total);
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.
let numbers: number[] = [10, 20, 30, 40];
// Print every number
for (let number of numbers) {
console.log(number);
}
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.
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.
// 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.
// Store student marks
let marks: number[] = [
80,
90,
75,
88
];
The comment describes the purpose of the array.
// Store student information
let student: {
name: string;
age: number;
} = {
name: "Rahul",
age: 21
};
// Define the structure of a student
interface Student {
name: string;
age: number;
marks: number;
}
The comment describes the purpose of the interface.
Comments can also be used in React TypeScript projects.
// Display welcome message
function Welcome() {
return <h1>Welcome</h1>;
}
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.
// Calculate the final price after quantity
let total = price * quantity;
// Add two numbers
let a = 10;
let b = 20;
A good comment should provide useful information rather than simply repeat obvious code.
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.
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;
}
// 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.
// 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.
| Type | Syntax | Use |
|---|---|---|
| Single-line | // comment |
Short explanation |
| Multi-line | /* comment */ |
Longer explanation |
| Inline | let age = 20; // age |
Short explanation after code |
Follow these practices when writing comments:
// creates a single-line comment./* */ creates a multi-line comment.Question: Which symbol is used to create a single-line comment in TypeScript?