The Power of Javascript: Basic Concepts - Javascript Comments
(Page 2 of 6 )
Comments are descriptive text that is ignored by the interpolator, which we use to comment our scripts. Comments in Javascript take two forms. The first form is a comment that takes one line. You use two forward slashes for it (//) followed by your comment, like the following:
// the firstValue is a location in memory that
// stores the value 10
Note that in this example the comment has spanned two lines, so we needed two forward slashes at the beginning of each line. As you can see, the whole line is used for the comment, but you can put your comment after the Javascript code line, as in the following line:
var secondValue = 23; // the secondValue is another location in memory that stores the value 23
The second form of the comment you can use is the multiple line comment, which you define by using a forward slash followed by an asterisk (/*), and end using an asterisk followed by a forward slash (*/). All of the lines between those symbols are considered a comment, like the following:
/* Comments, Keywords, Identifiers,
Case Sensitivity, White spaces and statements*/
Also, it can be written like this:
/*
Comments, Keywords, Identifiers,
Case Sensitivity, White spaces and statements
*/
At first you may not appreciate the importance of including comments in your scripts, but when you gain enough experience to write complex Javascript, you definitely will need to comment. Imagine that you have written a very important script for your company's website and after one year your manager asked you to modify the functionality of the script to do something else. You will open your script and may ask yourself "Why did I write the script this way?" and find that you don't remember. You may spend many hours trying to understand why and how your wrote the code that way.
To give another possible situation, say you wrote the script and after one year, while you are on vacation, your manager asked your co-programmer to modify this script. He will spend more hours than you to try to understand your code. So for you and for anyone else who might use your code, it's better to include comments in your scripts; that way, when you come back later you understand what's going on. You (or anyone else who needs to modify the code) can just read the scripts and comments and easily do what needs to be done.
However, when you are done with your script, it's better to remove the comments from the Web page (to minimize the page size and thus save bandwidth). You can write your scripts and save them in a Javascript file (with the extension .js), and name it after the Web page or any other name to remind you that this script is written for that page. In this file you can comment your code as much as you want, then you can copy the script (without the comments) into the Web page.
Next: Javascript Statements >>
More JavaScript Articles
More By Michael Youssef