How to Comment in JavaScript

What you’ll build or solve

You’ll add comments to a JavaScript file without changing how it runs.

When this approach works best

Use comments when you need to add context that code alone does not show.

This approach works best when you:

  • Explain a tricky condition, edge case, or business rule.
  • Leave a short note for future work, like a TODO.
  • Temporarily disable a line or block while testing a fix.

Skip heavy commenting when:

  • You describe obvious behavior line by line.
  • The code already reads clearly with good names.

Prerequisites

  • A .js file, or an HTML file with a <script> tag
  • Basic familiarity with JavaScript syntax

Step-by-step instructions

1) Choose the right comment syntax

JavaScript has two comment syntaxes. Pick the one that matches what you need to write.

Option A: Use // for single-line comments

Use this for short explanations or notes.

// This variable stores the user's age
constage=28;

constisLoggedIn=true;// Track login status

console.log(age,isLoggedIn);

Option B: Use /* */ for multi-line comments

Use this when you need more space than one line.

/*
 Calculate the final price after discount.
 Assumes the discount is a percentage.
*/
functioncalculatePrice(price,discount) {
returnprice-price* (discount/100);
}

What to look for

  • Close every /* with /.
  • Do not nest /* */ comments. JavaScript does not support that.
  • Inside <script> tags, use // or /* */, not HTML comments.

Examples you can copy

Example 1: Explain a non-obvious condition

consttemperature=30;

// Too hot for an outdoor run today
if (temperature>28) {
console.log("Workout indoors");
}

Example 2: Document a function with JSDoc

JavaScript

/**
 * Format a user's full name.
 * @param {string} firstName
 * @param {string} lastName
 * @returns {string}
 */
functionformatName(firstName,lastName) {
return`${firstName}${lastName}`;
}

Example 3: Leave a TODO note

// TODO: Replace hardcoded tax rate with a value from settings
consttaxRate=0.2;

functioncalculateTax(amount) {
returnamount*taxRate;
}

Common mistakes and how to fix them

Mistake 1: Forgetting to close a multi-line comment

What you might do:

/*
const name = "Alex";
console.log(name);

Why it breaks:

The missing */ makes JavaScript treat the rest of the file as a comment.

Fix:

/*
const name = "Alex";
console.log(name);
*/

Mistake 2: Nesting multi-line comments

What you might do:

/*
Outer comment
/*
Inner comment
*/
*/

Why it breaks:

Block comments cannot be nested, so the parser gets confused.

Fix: Use // inside the block comment instead.

/*
Outer comment
// Inner comment
*/

Mistake 3: Using HTML comments in JavaScript

What you might do:

<!-- console.log("Hello"); -->

Why it breaks:

<!-- --> is not valid JavaScript comment syntax in modern setups.

Fix:

// console.log("Hello");

Troubleshooting

If you see “Unexpected end of input,” look for an unclosed /* comment near the end of the file.

If a big chunk of code seems ignored, search for a stray /* or */ in the wrong place.

If your editor shows odd highlighting, remove the last comment you added and retype it carefully.

If comments appear as text on the page, make sure they are inside <script> tags.


Quick recap

  • Use // for single-line comments.
  • Use /* */ for multi-line comments.
  • Never nest block comments.
  • Always close /* with /.
  • Use comments to explain intent, not obvious code.