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 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.
Aside from the comments shown above in the "JavaScript Container", DO NOT ever put any other HTML comments inside your JavaScript code. This will cause many errors, as the characters used for the HTML comments have very different meanings in JavaScript.
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 as you write your code is the best practice.
Pseudocode
Many programmers make use of something called "pseudocode" ("not real code"), that helps them to write down what the programming will do in plain English (or your language of choice). This can also be done with comments. Some programmers will write all their pseudocode in the JavaScript comments, then add in the real code where applicable. You will see examples of this type of practice in later code samples throughout this course.
Commenting Your Code Summary
- It is always a good idea to comment your code.
- Single lines of JavaScript can be commented out using a double forward slash
//. - Multiple lines of code can be commented by using
/*and*/. - You can also comment out parts of a single line with
/*and*/. - It is often a good idea to use pseudocode in JavaScript comments to explain what the script will do before it is written.
Coming Soon:
- Feb 13
- ISSD 24 - XML
- Feb 21
- ISSD 23 - Web 2.0 Technology
- Feb 23
-
Facebook for Business
Hours: 2
Cost: Free!
- Feb 25
-
Search Engine Optimization (SEO)
Hours: 2
Cost: Free!
- Feb 25
-
Search Engine Marketing (SEM)
Hours: 2
Cost: Free!
- Mar 05
- ISSD 23 - Ajax
DHTML - TOC - Introduction - Books -
JavaScript Programming Fundamentals - Links - Questions -
[ 1 ] - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 -
