[JavaScript]
[Previous page] [Section contents page] [Next page]
Comments

I cannot insist too much on the importance of using comments to annotate your JavaScripts. In the Guide to HTML, I stressed the importance of using comments to help others (or remind yourself!) what function various parts of your code serve; because JavaScript is a much more complex and abstract language than HTML, the importance of comments here is doubly important. The failure to document your scripts will greatly increase your own work when you (inevitably!) need to modify your scripts later, and render them all but useless to another developer who needs to use and modify your scripts.

JavaScript allows two different kinds of comments that will be useful for different purposes:

  • /* multi-line comments that are enclosed like this, and may extend over as many lines as you like. Note, however, that these kinds of comments should not begin or end on the same line as other script code */
  • // here-to-the-end-of-the-line comments like this

The first kind of comment, contained between the delimiters /* */, is useful for extended explanations of a block of code. The second kind, which has only a // at the beginning, is useful for inserting quick annotations on the purpose of individual statements. Note that the second kind of comment can be added to the end of a line of regular code, like this:

	var URL = unescape(location.href) // get URL in ASCII form
	
[Previous page] [Section contents page] [Next page]