Account Log In - Home/Outlines - Books - Contact Us - Support - Employment -

DHTML

JavaScript Programming Fundamentals

Commenting Your Code

NOTE: From this point on, we will be showing you JavaScript examples. As you may remember from the last section, we talked about where to put JavaScript on a Web page. JavaScript always should go between the <script> tags in the following way:

<script language="JavaScript" type="text/javascript">
<!--
//script goes here
//-->
</script>

All the JavaScript examples shown in the course from here on will assume that the code is contained inside this text. In some instances, we may modify this a bit, but we will make mention of that if we do.

Anyone who is familiar with HTML knows about the HTML comment tag.

<!-- This is an HTML comment. The user will never see this unless they view the source code -->

It is used to comment out parts of the HTML code so that it will not show up in the browser window. The browser basically ignores anything written between these tags. Web developers commonly use them to make notes on different design areas, or even to tell other designers who may be viewing the source code special information. There are many reasons that people use comment tags.

JavaScript also has its own commenting abilities. Obviously, JavaScript does not use the same comment tags as HTML, but like HTML, JavaScript will ignore any content that is commented.

Why Comment?

Commenting code is a common thing that programmers forget to do. Even the most advanced programmers for any programming language can go back to some code that they had written years or even months ago, and have no clue what they wrote. Commenting not only helps you to make sense of previous code, it can also help you to organize you code, so you are better able to troubleshoot it when you need to.

There are other reasons to comment out code as well. You may be writing a custom script for someone else, and may want to include instructions for them to modify it to suit their needs. You may also want to "comment out" code when troubleshooting it. "Commenting out" means that you place comments before a particular line or block of code that you want to temporarily disable. This saves you the trouble of having to delete it, then put it back in later.

Commenting a Single Line

If you only want to comment a single line in JavaScript, you can use a pair of forward slashes //. Any code after these two slashes will be ignored. The following line will be read normally.

// This is a single line JavaScript comment. It will be ignored by the browser.
// This is another line of JavaScript comment. You can have as many of these as you wish.

Commenting code in this way is useful, but impractical if you want to comment out large blocks of code, or write a lot of comments.

Commenting Multiple Lines

You can comment out multiple lines of code in JavaScript in the following way:

/*
This is a block of commented code
Everything you put here will be ignored
until the browser sees the closing comment.
This is rather similar to how the HTML comment tags work.
*/

Note that the beginning comment /* is different than the end comment */.

These are commonly referred to as block comments.

Another good use for doing this kind of commenting is when you want to comment part of a line. The // comment would comment out the rest of the line, whether you wanted it to or not.

var my_var = 10; /* this part of the line is commented out */ my_var = my_var + 1;

It is a very good idea to continuously comment out your code as you go, but if you really feel this slows you down, then you should at least comment out your code when you are finished writing it. If you wait too long though, you may not remember as well what you had done.

Commenting Your Code Summary

Free seminars, open to the public:
Free seminars, open to the public:


DHTML - TOC - Introduction - Books -
JavaScript Programming Fundamentals - Links - Questions - Quiz -
[ 1 ] - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 -

DHTML - TOC - Introduction - Books -
JavaScript Programming Fundamentals - Links - Questions - Quiz -
[ 1 ] - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 -