Home Videos Exercises MCQ Q&A Quiz E-Store Services Blog Sign in Appointment Payment

JavaScript Comments

JavaScript comments can be used to explain JavaScript code, and to make it more readable.

JavaScript comments can also be used to prevent execution, when testing alternative code.


Single Line Comments

Single line comments start with //.
Any text between // and the end of the line will be ignored by JavaScript (will not be executed).

Example of single line comment:
// Change paragraph: document.getElementById("p1").innerHTML = "This is paragraph";
// Change heading: document.getElementById("h1").innerHTML = "This is heading";


Multi line comments

Multi-line comments start with /* and end with */.

Any text between /* and */ will be ignored by JavaScript.
Example:
/* this is multi line comments Soopro pathshala provides online and offline training on various courses */


In JavaScript, comments are used to explain code, improve readability, or temporarily disable parts of code during debugging. There are two types of comments:


1. Single-Line Comments

Start with //.
Anything after // on the same line is ignored by the JavaScript engine.


2. Multi-Line Comments

Start with /* and end with */.
Can span multiple lines and are often used for detailed explanations or to comment out large blocks of code.


Best Practices for Comments:

1. Keep comments concise and relevant.

  • Avoid stating the obvious in comments.

  • 2. Use comments to explain why rather than what.

    • Focus on the reasoning behind the code instead of describing the code itself.

    3. Use comments for complex logic.

    • When implementing non-obvious solutions, explain your approach.

    4. Avoid excessive commenting.

    • Well-written code with meaningful variable and function names often needs fewer comments.