Comments in JavaScript

JavaScript comments explain code for people and are ignored by the JavaScript engine. Use // for a single-line comment and /* ... */ for a block comment that can span multiple lines.

Basic syntax:

// Single-line comment
const name = "plus2net";

/*
  Block comment
  across multiple lines
*/
console.log(name);

Single-Line JavaScript Comments Top ↑

Everything after // on that line is treated as a comment, unless those characters are inside a string or another token where they have a different meaning.

// Store the initial score
let score = 0;

// Increase it after a correct answer
score += 1;

Comments at the End of a Line Top ↑

A short comment can follow a statement. Use this sparingly; if the explanation is long, put it on its own line.

const timeout = 5000; // milliseconds
const retries = 3;         // maximum retry attempts

Multi-Line or Block Comments Top ↑

Block comments start with /* and end with */. They can span several lines.

/*
  Calculate the final amount after discount.
  The percentage is supplied as a whole number.
*/
function applyDiscount(price, percent) {
  return price * (1 - percent / 100);
}

Can JavaScript Block Comments Be Nested? Top ↑

No. Normal JavaScript block comments do not nest. The first */ closes the comment.

/* Outer comment
   /* Do not start another block comment here */
   // The outer comment has already been closed above.
*/
Tip: use several // lines when you need commentary that might otherwise contain /* ... */ sequences.

Using Comments to Temporarily Disable Code Top ↑

Comments can temporarily prevent a small piece of code from running while debugging, but version control is a better long-term record than leaving large amounts of dead code in production files.

const total = 25;
// console.log("Temporary diagnostic:", total);
console.log(total);

Documentation Comments Top ↑

JavaScript itself treats a documentation-style block such as /** ... */ as an ordinary comment. Tools such as JSDoc and editors can interpret structured tags inside it.

/**
 * Add two numbers.
 * @param {number} a First number.
 * @param {number} b Second number.
 * @returns {number} Sum of a and b.
 */
function add(a, b) {
  return a + b;
}

Use documentation comments where they help consumers understand a reusable API; do not document every obvious line.

JavaScript Comments and HTML Comments Are Different Top ↑

Use JavaScript comment syntax inside JavaScript. Do not wrap modern scripts in the old HTML-comment pattern <!-- ... //-->. That pattern belonged to very old browser compatibility techniques and should not be copied into current code.

What Makes a Useful Code Comment? Top ↑

  • Explain why a non-obvious decision exists, not merely what readable code already says.
  • Document assumptions, edge cases, units, compatibility constraints, or business rules.
  • Keep comments synchronized with the code; an outdated comment can be worse than no comment.
  • Prefer clearer names and smaller functions before adding comments to explain unnecessarily complicated code.
  • Do not put secrets, passwords, tokens, or private information in comments committed to source control.

Common Comment Mistakes Top ↑

  • Using HTML comments instead of JavaScript comments inside a script.
  • Trying to nest /* ... */ comments.
  • Commenting out large obsolete sections indefinitely instead of removing them through version control.
  • Writing comments that repeat the statement without adding meaning.
  • Allowing a comment to describe behavior the code no longer has.

Continue with adding JavaScript to HTML, variables, and functions.




Subscribe to our YouTube Channel here



plus2net.com







Francis

08-04-2010

I really loved this website guys.



✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer