How to Write Single and Multi Line Comments in JavaScript

Learn the two ways to write comments in JavaScript: single-line comments and block comments. See clear examples and know when to use each style.

4 min read

JavaScript comments are notes in your source code that JavaScript ignores when it runs. Use them to explain intent, warn about a tricky decision, or temporarily skip code while debugging.

JavaScript has two comment styles: single-line comments for short notes and multi-line comments for longer blocks.

Single-Line Comments

A single-line comment starts with two forward slashes. Use it when the note belongs to one line of code or one short idea.

javascriptjavascript
// This line is a comment
console.log("Hello!"); // This note is ignored

The console prints Hello!. The first line is skipped, and the note after the log statement is ignored.

Single-line comments are useful for quick notes, short warnings, or temporarily disabling one line:

javascriptjavascript
let maxAttempts = 3; // Limit failed login attempts
 
// console.log("Current user:", user);

The first comment explains why the number exists. The second line is a disabled debug log, which is useful while testing but should not stay in finished code.

Multi-Line Comments

A multi-line comment starts with a slash-star marker and ends with a star-slash marker. Everything between those markers is ignored, no matter how many lines it spans.

javascriptjavascript
/*
  This whole block is a comment.
  JavaScript skips each line inside it.
*/
console.log("Code after the comment still runs.");

The console prints Code after the comment still runs. The comment block itself produces no output.

Use multi-line comments for longer explanations, file notes, or temporarily disabling a small block while debugging:

javascriptjavascript
/*
  Calculate the final order total after discounts.
  The returned value should already include tax.
*/
function calculateOrderTotal() {
  return 42.5;
}

The comment gives context before the function. The function is still complete and runnable, so the example does not rely on placeholder code.

Disabling Code with Comments

During debugging, comments can help you skip a line or block without deleting it:

javascriptjavascript
console.log("Step 1: Starting");
 
/*
console.log("Step 2: Temporarily disabled");
console.log("Step 3: Also disabled");
*/
 
console.log("Step 4: Finished");

The console prints Step 1 and Step 4 only. Steps 2 and 3 are skipped because they sit inside the multi-line comment.

This is useful when you want to isolate a bug. Remove disabled code before committing if it no longer teaches or protects anything.

Common Mistakes

Nested Multi-Line Comments

You cannot put a multi-line comment inside another multi-line comment. The first closing marker ends the whole comment, and anything after it becomes regular source text again.

javascriptjavascript
/*
  Outer comment starts here
  /*
    This inner block does not work
  */
  This line is no longer safely commented
*/

This can cause confusing syntax errors. If a block already contains multi-line comments, use single-line comments on each line or remove the inner block first.

Comment Markers Inside Strings

Comment markers inside a string are just text:

javascriptjavascript
const url = "https://example.com";
const pattern = "// not a real comment";
 
console.log(pattern);

The output is the string text itself: // not a real comment. JavaScript only treats comment markers as comments when they appear outside strings.

Outdated Comments

The most harmful comment is one that lies. If you change code but forget to update the comment, the comment becomes misleading.

javascriptjavascript
// Add 10% tax
const taxRate = 0.08;

The comment says 10 percent, but the value is 8 percent. Update the comment or remove it.

Single-Line vs Multi-Line

StyleSyntaxBest For
Single-line// commentQuick notes, inline explanations, disabling one line
Multi-line/* comment */Longer notes, file descriptions, disabling blocks

For most everyday commenting, single-line comments are enough. Reach for multi-line comments when the note needs more than one line.

Once you are comfortable with comments, see how to run JavaScript in the browser and Node.js to try these examples yourself. A JavaScript code style guide can also help you keep comments consistent across a project.

Rune AI

Rune AI

Key Insights

  • Single-line comments start with two slashes and end at the line break.
  • Multi-line comments start with a slash-star marker and end with a star-slash marker.
  • Comments are ignored by JavaScript and do not run as statements.
  • Use comments to explain why, not what the code already says.
  • Multi-line comments cannot be nested inside other multi-line comments.
RunePowered by Rune AI

Frequently Asked Questions

Do comments affect how fast my JavaScript runs?

No. Comments are ignored as source text and do not execute as JavaScript statements.

Can I nest multi-line comments inside other multi-line comments?

No. JavaScript does not support nested multi-line comments. The first closing marker ends the comment, which can cause syntax errors if you try to nest them.

Should I delete comments before putting code into production?

Not manually. Modern build tools often remove comments from production bundles. You can keep useful comments in source code.

Conclusion

Comments are one of the simplest tools in JavaScript, but using them well makes your code easier to read, debug, and share. Single-line comments handle quick notes and disabled code, while multi-line comments work for longer explanations and temporary block removal. Pick the right style for the job and keep comments clear and current.