Changes

Jump to: navigation, search

Comments in JavaScript

751 bytes added, 15:01, 19 April 2007
no edit summary
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 explain what a section of your script does then you must have written is 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 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:
 
<pre>
// 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
</pre>

Navigation menu