Comments in JavaScript

From Techotopia
Revision as of 14:14, 30 May 2007 by Neil (Talk | contribs) (Single Line Comments)

Jump to: navigation, search

There is an old saying amongst veteran programmers that goes something like "Don't comment bad code, re-write it!". Before exploring what these seasoned programmers are really saying, it is important to understand what comments are.

Comments in both programming and scripting languages provide a mechanism for the developer to write notes that are ignored by the compiler or interpreter. These notes are intended solely for either the developer or anyone else who may later need to modify the script. The main purpose of comments, therefore, is to allow the developer to make notes that help both anyone who may read the script later to understand issues such as how a particular section of a script works, what a particular function does or what a variable is used to store. Commenting a script is considered to be good practice. Rest assured that a section of script that seems obvious when you write it will often be confusing when you return to it months, or even years later to modify it. By including explanatory comments alongside the script this becomes less of a problem.

Now, back to that old saying - "Don't comment bad code, re-write it!". What this phrase suggests is that if code is well written in the first place you do not need comments to explain what it does and how it does it. It also suggests that if you are having to write lots of comments to explain what a section of your script does then you must have written it badly. Whilst one should always strive to write good code there is absolutely nothing wrong with including comments to explain what the code does. Even a well written script can be difficult to understand if it is solving a difficult problem so, ignore the old programmers adage and never hesitate comment your JavaScript scripts.

Another useful application of comments in JavaScript is to comment out sections of a script. By putting comment markers around sections of a script ensures that they are not executed by the interpreter when the web page is loaded. This can be especially useful when you are debugging a script and want to try out something different, but do not want to have to delete the old code until you have tested the new code actually works.

Single Line Comments

The mechanism for a single line comment borrows from the C++ and Java langauges by prefixed the line with //. For example:

// This is a comment line. It is for human use only and is ignored by the JavaScript Interpreter.

var myString = "Hello";

// This is another comment

The // syntax tells the intepreter that everything on the same line following on from the // is a comment and should be ignored. This means that anything on the line before the // comment marker it is not ignored. The advantage of this is that it enables comments to placed at the end of a line of scripting. For example:


var myString = "Welcome to Technotopia"; // Variable containing welcome string

In the above example everything after the // marker is considered a comment and, therefore, ignored by the JavaScript interpreter. This is provides an ideal method for palcing comments on the same line of script that descript what that particular script line does.

Multi-line Comments

For the purposes of supporting comments that extend over multiple lines JavaScript borrowed some syntac from the C programming language. The start and end of lines of comments are marked by the /* and */ markers respectively. Everything immediately after the /* and befre the */ is considered to be a comment, regardless of where the markers appear on a line. For example:


/*
This Function adds two numbers together
and returns the result of the addition
*/
function (num1, num2)
{
return (num1 + num2)
}

Multi-line comments are particularly useful for commenting out sections of a script you no longer wish to run but do not yet wish to delete, together with an explanation of when and why you have comment it out:

/*

Commented out December 23 while testing improved version

var myValue = 1;

var myString = "My lucky number is ";

document.writeln ( mayString + myValue );

*/

In the above example everything between the /* and */ markers is considered to be a comment. Even though this content contains valid JavaScript it is ignored by the interpreter.


Summary

Comments in JavaScript enable the developer to add notes about the script or comment out sections of script that should not executed by the interpreter. Comments can be single line comments (using the // marker) or multi-line (beginning with /* and ending with */).

Commenting is considered to be good practice. Regardless of how well you understand the logic of some JavaScript there is a good chance you may one day have to return to that script and modify it. Often this can be months, or even years, later and what seemed obvious when you at the time you wrote it may seem less obvious in the future. Also, it is often likely that some other person will have to work on your scripts in the future. For both these reasons it is a good idea to provide at least some basic amount of commenting in your scripts.