JavaScript comments: Single-line and block syntax

Use // for a JavaScript comment up to the end of the line, or /* and */ around a block comment. Comments document decisions or temporarily disable code. Anyone who receives the source can read them.

Write single-line and block comments

// This is a comment.
const count = 3; // A comment can follow code.

/*
  A block comment can span multiple lines.
  It ends at the closing marker.
*/
const enabled = true;

Code before // still runs. A block can span one or more lines. Leading asterisks inside a block are a style convention. See JavaScript lexical grammar for how comments and whitespace are parsed. Use comments to explain units, decisions, and conditions the code alone does not show.

Change one comment in a complete example

Save the full example in UTF-8 as comments.html and open it. The initial result is Total: 45 minutes from 15 × 3. Remove only // from // total += 5;, save and reload to get 50. Put it back to restore 45.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Practice JavaScript comments</title>
</head>
<body>
<h1>Change an example with comments</h1>
<p id="output"></p>
<pre id="markers"></pre>
<script>
const minutes = 15; // Minutes per session
const sessions = 3;
/*
  The base total includes work time only.
  Uncomment the line below to add five minutes.
*/
let total = minutes * sessions;
// total += 5;
document.getElementById("output").textContent = `Total: ${total} minutes`;

// Markers inside these quotes are part of the strings.
const lineMarker = "//";
const blockMarker = "/* note */";
document.getElementById("markers").textContent =
  `Markers in strings: ${lineMarker}, ${blockMarker}`;
</script>
</body>
</html>

The strings // and /* note */ printed underneath contain literal markers. A global replacement of // could also change URLs inside strings.

Check nesting, language syntax, and visibility

Block comments end at the first */, so they cannot nest. To disable a range that already contains a block comment, use the editor’s line-comment command. HTML uses <!-- ... --> outside a script. A comment does not replace a required operator or bracket.

const total = 2 /* first value */ + 3; // 5
const text = "// these markers are part of the string";

Readers can inspect comments in scripts delivered to their browser. Keep passwords, API secrets, and personal data out of client code. Review comments when code changes so the explanation stays accurate.

When a line break inside a comment changes return

Run the next code in the Console. A block comment containing a line break counts as a line terminator between return and its expression. Under automatic semicolon insertion, that return ends without a value.

{
  function inlineComment() {
    return /* no line break */ 5;
  }

  function multilineComment() {
    return /* this comment
      contains a line break */ 5;
  }

  function groupedExpression() {
    return (
      /* the expression starts on the return line */
      5
    );
  }

  console.log(inlineComment());      // 5
  console.log(multilineComment());   // undefined
  console.log(groupedExpression());  // 5
}

The output is 5, undefined, and 5. Move a long comment above return, or put an opening parenthesis on the return line and break lines inside it. When a return value is missing, check for a line break before the expression.