Learn Javascript
Comments
Join the Discussion
More of this Tutorial
Introduction.
You may do okay when you write your Javascript code in the first place but coming back to it later to make changes may be difficult if you can't remember what the particular statements are meant to do. You can resolve this to some extent by using meaningful names for all of your variables and functions but sometimes this just isn't enough to tell you just what the statements are doing.
Additionally when you start to write significant Javascripts you will want to include a copyright notice in the script and perhaps instructions on how the script is to be used.
Adding comments to your Javascript will solve these problems.
Single Line Comments
The simplest way to add a short comment to your Javascript is to precede the comment text with two slashes. This is an instruction to the browser that the rest of this line is a Javascript comment and can be ignored.
You can add comments to the end of your statements using this method or specify the comment on a line by itself. If you are going to place comments on lines by themselves then the comment should be above the line or lines of code to which it refers. Here are a couple of example comments:
alert('x'); // and this one is a part line comment
You can also temporarily comment out single lines of Javascript code by adding the // to the front of the line like this:
Multi Line Comments
There will be times when what you want to say doesn't fit on one line and so you will need to create multiple comment lines. There are two ways you can do this. One that you have already learned in this tutorial is to just place lots of single line comments one after the other like this:
// copyright 3rd September 2004, by Stephen Chapman
// permission to use this Javascript on your web page is
// granted provided that all of the code in this script (including
// these comments) is used without any alteration
The problem with this method is that you need to place the // on the front of each line. There is an alternative way of defining a comment that allows us to just specify where the comment starts and finishes. To do this we would recode the above comments as follows:
copyright 3rd September 2004, by Stephen Chapman
permission to use this Javascript on your web page is
granted provided that all of the code in this script (including
these comments) is used without any alteration */
The /* */ surrounds a comment that goes over more than one line. You will also find this method useful if you want to temporarily comment out multiple lines of code.
You can nest single line comments inside of multi line comments so you don't need to worry about any single line comments within a block of code that you are temporarily commenting out. What you can't do is to nest one multi line comment within another. The first */ found will terminate the comment regardless of how many /* preceded it.
There is no need to go overboard with adding comments to your code. Self documenting your code by using meaningful variable and function names is by far the better alternative since they are more likely to remain correct when the code is changed at a later date. Changing comments to reflect changes in code is something that is often neglected.
Using What You Know
If you have obtained a prewritten script or have written one yourself and can't get it to work at all then there is probably an error stopping the script from starting to run. One way to find the error is to temporarily comment out a section of code and try the script again. If the uncommented part of the script now works or you get an error saying that some variable or function in the commented out part doesn't exist then chances are that the problem is within the commented code. Uncomment the code and try commenting a smaller section to narrow down where the problem is.

