Advertisement
Online Test Banks
Score higher
See Online Test Banks
eLearning
Learning anything is easy
Browse Online Courses
Mobile Apps
Learning on the go
Explore Mobile Apps
Dummies Store
Shop for books and more
Start Shopping

How to Write Comments in JavaScript for HTML Integration

Comments are an important part of your JavaScript application. While you may easily remember what an application does for a day or two after you write it, trying to figure out what complex applications are doing one or two years later can prove difficult.

In fact, there are documented cases where companies have had to rewrite applications from scratch because no one could figure out how the application worked due to a lack of comments. No one has an infallible memory, so comments are an important way for you to keep track of what your application does.

JavaScript doesn’t care about your comments. It ignores them, so you can write the comments any way that you want. The content is there to help you. Good comments explain details such as

  • Why you wrote a particular function or other code block

  • What task the code block is supposed to perform

  • Who requested the code block

  • Why you used a particular technique for writing the code block

  • Which resources you used to create the code

  • How the code was created and tested

  • Who worked on the code (including contact information)

  • When the code was created

A growing number of people insist that some forms of code are self-documenting. However, JavaScript is never self-documenting, and if you leave out the comments, you may find yourself working late nights and weekends trying to figure out your code later.

How to create single-line comments in JavaScript

Most developers use what are called single-line comments. A single-line comment can appear anywhere on a line. To create a single-line comment, you type two forward slashes (//) and then the comment text. Here are examples of single-line comments.

function WriteText()
{
  // Change the first statement, then wait so the
  // user can see the change.
  document.getElementById("First").innerHTML =
   "Changed First";
  alert("Check First!"); // Wait here.
 
  // After the user clicks Click Me, show the
  // change to the second statement.
  document.getElementById("Second").innerHTML =
   "Changed Second";
}

Each line begins with a double slash (//) to indicate that it’s a comment line. Notice that you can begin a comment after the code in a line, as shown in the line with the alert() call in it. Everything after the double slash is treated as a comment.

Use white space to help delineate sections of code that are covered by a single comment. The addition of white space makes the comments easier to find, and it also makes it easier to see how much code the comment affects.

How to create multi-line comments in JavaScript

Sometimes you have more than one or two lines worth of comment to write. In this case, you can create multi-line comments. A multi-line comment begins with a slash and an asterisk (/*) and ends with an asterisk and slash (*/). Here’s an example of a multi-line comment.

<script language="JavaScript">
  /* This function makes changes to the text on the
  * page after the user clicks Click Me as a
  * demonstration of modifying page elements using
  * multiple lines of code.
  *
  * Written by: John Mueller */
 
  function WriteText()
  {
   // Change the first statement, then wait so the
   // user can see the change.
   document.getElementById("First").innerHTML =
     "Changed First";
   alert("Check First!"); // Wait here.
  
   // After the user clicks Click Me, show the
   // change to the second statement.
   document.getElementById("Second").innerHTML =
     "Changed Second";
  }
</script>

In this case, the comment is used to create a description of the function. These blocks often appear in team projects to help members interact with each other. For example, this comment would tell someone on the team who wrote the block of code shown in the example.

How to prevent code execution with comments

Developers often use single-line comments to prevent a line of code from executing. Commenting out code is a common technique where a developer adds two forward slashes (//) in front of a line of code that the developer suspects is causing problems in the application. Professional code editors often include features that make it easy to comment out lines of code and add them back into the application as needed.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win an iPad Mini. Enter to win now!