JavaScript CommentsGet Certified
Comments are notes that you write inside your code. JavaScript completely ignores them when the program runs. They exist only to help humans understand the code.
Single-line Comments
A single-line comment starts with two forward slashes
//. Everything after the // on that line is
ignored.
// This is a single-line comment
let name = "Michael"; // You can also put a comment at the end of a line
console.log(name);
// is ignored by the JavaScript engine.console.log statement actually runs.Multi-line Comments
Multi-line comments start with /* and end with
*/. Everything between them is ignored, even if it
spans many lines.
/*
This is a multi-line comment.
It can span as many lines as you need.
Useful for longer explanations.
*/
let age = 28;
console.log(age);
/* and */ is ignored, no matter how many lines it covers.Commenting Out Code
A very common use of comments is to temporarily disable a line or block of code without deleting it.
let score = 100;
console.log("Score:", score);
// console.log("This line is temporarily disabled");
/*
console.log("These lines are also disabled");
console.log("They will not run");
*/
// at the start turns the statement into a comment./* */ disables them all at once.Documentation Comments (JSDoc)
JSDoc-style comments start with /** and are used to
document functions, parameters, and return values. Many editors use
them to provide better autocomplete and tooltips.
/**
* Adds two numbers together.
* @param {number} a - The first number.
* @param {number} b - The second number.
* @returns {number} The sum of a and b.
*/
function add(a, b) {
return a + b;
}
console.log(add(5, 3));
/** */ comment describes the function, its parameters, and what it returns.Comment Syntax Overview
| Type | Syntax | Spans Multiple Lines? |
|---|---|---|
| Single-line | // comment | No |
| Multi-line | /* comment */ | Yes |
| Documentation (JSDoc) | /** comment */ | Yes |
When Should You Use Comments?
Good comments explain the why behind a piece of code, not just the what. The code itself already shows what is happening; a good comment adds context that isn’t obvious from the code alone.
// Not very useful: restates the obvious
let age = 28; // set age to 28
// More useful: explains the reasoning
let retryLimit = 3; // API is unreliable, so allow up to 3 retries
console.log(age, retryLimit);
Practical Example
Let’s combine different comment styles in a small practical example.
/**
* Calculates the final price after applying a discount.
* @param {number} price - The original price.
* @param {number} discountPercent - Discount percentage (0-100).
* @returns {number} The discounted price.
*/
function applyDiscount(price, discountPercent) {
// Convert the percentage into a decimal multiplier
const multiplier = 1 - (discountPercent / 100);
return price * multiplier;
}
console.log(applyDiscount(200, 25)); // Expected: 150
Common Mistakes With Comments
- Trying to nest multi-line comments, which causes a syntax error.
- Writing comments that simply repeat what the code already says, adding no real value.
- Leaving outdated comments that no longer match the current code.
- Forgetting to close a multi-line comment with
*/. - Overusing comments instead of writing clear, self-explanatory code.
Best Practices for Comments
- Explain why the code does something, not just what it does.
- Keep comments short and to the point.
- Update or remove comments when the related code changes.
- Use JSDoc-style comments for functions that are reused or shared with other developers.
- Avoid commenting every single line; reserve comments for places that genuinely need clarification.
Quick Summary
- Comments are ignored by JavaScript and exist only for humans reading the code.
- Single-line comments start with
//. - Multi-line comments are wrapped between
/*and*/. - Multi-line comments cannot be nested inside each other.
- JSDoc-style comments (
/** */) document functions and can power editor autocomplete. - Good comments explain reasoning, not just restate the code.
Try It Yourself
Add your own single-line and multi-line comments to the code below,
and try commenting out the second console.log line.
// Try adding a comment above this line
let name = "Michael";
console.log(name);
console.log("Hello, " + name + "!");
console.log lines.Tip: A good comment tells future readers of your code (including future you) something the code itself cannot: the reasoning behind a decision, a warning about a tricky edge case, or a note about why a simpler approach wasn’t used.
Understanding comments is a small but important step before moving on to conditionals, loops, functions, and more advanced JavaScript concepts.
🧪 Test Your JavaScript Code
Edit the code on the left and click “Run Code” to see the console output on the right.
Click “Run Code” to see the output here.
Code Explanation
The complete example stays visible. Use Previous and Next to move through its logical blocks.
Select Next to begin exploring the code.