HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

JavaScript Comments

JavaScript comments are notes written inside your code that the browser ignores during execution. They do not affect how the program runs, but they help explain what the code is doing and make it easier for humans to read and maintain.


Why Use Comments in JavaScript?

Comments are useful when you want to describe code behavior, leave reminders, temporarily disable a line while testing, or make your code easier for other developers to understand.

Good comments help in three common situations: when you need to explain code that is not immediately obvious, when you want to make collaboration easier for other developers, and when you need to test something by temporarily disabling a line of code without deleting it.

At the same time, comments should support the code, not replace clear code. The best result comes from combining readable JavaScript with comments that add real meaning.


Single-Line Comments

A single-line comment begins with two forward slashes: //. Everything after // on the same line is treated as a comment and ignored by JavaScript.

// This is a single-line comment
let x = 5;

// Show the value in the console
console.log(x);

Single-line comments are best for short explanations or quick notes above a statement.


Single-Line Comment at the End of a Line

You can also place a single-line comment after a statement on the same line:

let price = 100; // product price in taka
let tax = 15; // tax amount
Note: End-of-line comments can be useful, but avoid making them too long. Very long inline comments make code harder to scan.

Multi-Line Comments

A multi-line comment starts with /* and ends with */. Anything between them is ignored by the browser.

/*
This is a multi-line comment.
It can span several lines.
It is useful for longer explanations.
*/
let message = "Hello";

Multi-line comments are helpful when you need to describe a larger block of code or leave a detailed explanation.


Using Comments to Prevent Code Execution

During testing, you can comment out a line of code so JavaScript will skip it. This is a common technique when debugging.

let firstName = "Nadia";
// let firstName = "Karim";

console.log(firstName);

In this example, only the active line runs because the second variable declaration is commented out.

Tip: Comments are useful for temporary testing, but do not leave large amounts of old commented-out code in real projects. It makes files messy and harder to maintain.

Good Comment vs Bad Comment

Helpful comments explain something that is not obvious. Unhelpful comments only repeat what the code already clearly says.

// Good: explain why this value is fixed for the discount campaign
let discount = 20;

// Bad: set discount to 20
let discountRate = 20;
Warning: Do not depend on comments to fix unclear code. First try to write clean, readable code. Then add comments only where extra explanation is genuinely useful.

Best Practices for JavaScript Comments

  • Write comments that explain why, not only what
  • Keep comments short, clear, and relevant
  • Update comments when the code changes
  • Avoid writing comments for very obvious lines
  • Use comments to separate important sections when a file grows larger

Summary Table

Comment Type Syntax Best Use
Single-line comment // comment Short notes and quick explanations
Multi-line comment /* comment */ Longer descriptions across multiple lines
Commented-out code // code or /* code */ Temporary testing and debugging

Key Points to Remember

  • Comments are ignored by the browser and exist only for people reading the code
  • Use // for single-line comments
  • Use /* ... */ for multi-line comments
  • Comments can help explain logic or temporarily disable code
  • Good comments add clarity, while unnecessary comments add noise
  • Always keep comments accurate and updated