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.
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.
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.
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
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.
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.
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;
| 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 |
// for single-line comments/* ... */ for multi-line comments