CA Resources

Comments

Learn how to create and manage JavaScript comments.

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

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

We can use single line and multi-line comments in JavaScript.

Single Line

Single line comments are the most often used method of placing comments.

<!DOCTYPE html>
<html>
  <body>
    <h1 id="myH"></h1>
    <p id="myP"></p>
    <script>
      // Change content of heading:
      document.getElementById("myH") = "JavaScript Comments";
      // Change content of paragraph:
      document.getElementById("myP") = "My First Paragraph";
    </script>
  </body>
</html>

Multi-line

Multi-line or block comments are used when documenting the project.

<!DOCTYPE html>
<html>
  <body>
    <h1 id="myH"></h1>
    <p id="myP"></p>
    <script>
      /*
        The code below will change 
        the heading with id = "myH"
        and the paragraph with id = "myP"
      */
      document.getElementById("myH").innerHTML = "JavaScript Comments";
      document.getElementById("myP").innerHTML = "My First Paragraph";
    </script>
  </body>
</html>
Edit on GitHub

Last updated on

On this page