JS References

JS Objects
JS HTML DOM

Suscriber with us



Receive HTML?

Syndicate

JS Comments PDF Print E-mail

JavaScript comments can be used to make the code more readable.

Have you ever written a script or a program in the past only to look at it 6 months later and have no idea what's going on in the code? You most likely forgot to do what all programmers tend to forget: write comments!

When writing code you may have some complex logic that is confusing, this is a perfect opportunity to include some comments in the code that will explain what is going on. Not only will this help you remember it later on, but if you someone else views your code they will also be able to understand the code (hopefully)!

Another great thing about comments is the capability for comments to remove bits of code from execution when you are debugging your scripts. This lesson will teach you how to create two types of comments in Javascript: single line comments and multi-line comments.

Creating Single Line Comments

To create a single line comment in Javascript you place two slashes "//" in front of the code or text you wish to have the Javascript interpreter ignore. When you place these two slashes, all text to the right will be ignored, until the next line.

These types of comments are create for commenting out single lines of code and writing small notes.

Javascript Code:

<script type="text/javascript">
<!--
// This is a single line Javascript comment
document.write("I have comments in my Javascript code!");
//document.write("You can't see this!");
//-->
</script>

Display:

I have comments in my Javascript code!

Each line of code that is colored red is commented out and will not be interpreted by the Javascript engine.

Creating Multi-line Comments

Although a single line comment is quite useful, it can sometimes be burdensome to use for large segments of code you wish to disable or extra long gasping comments you need to insert. For this large comments you can use Javascript's multi-line comment that begins with /* and ends with */.

Javascript Code:

<script type="text/javascript">
<!--
document.write("I have multi-line comments!");
/*document.write("You can't see this!");
document.write("You can't see this!");
document.write("You can't see this!");
document.write("You can't see this!");
document.write("You can't see this!");
document.write("You can't see this!");
document.write("You can't see this!");*/
//-->
</script>

Display:

I have multi-line comments!

Quite often text editors have the ability to comment out many lines of code with a simple key stroke or option in the menu. If you are using a particular text editor for programming be sure that you check and see if it has an option to easily comment out many lines of code!

Using Comments to Prevent Execution

In this example the comment is used to prevent the execution of a single code line:

<script type="text/javascript">
document.write("<h1>This is a header</h1>");
document.write("<p>This is a paragraph</p>");
//document.write("<p>This is another paragraph</p>");
</script>

Try it yourself.

In this example the comments is used to prevent the execution of multiple code lines:

<script type="text/javascript">
/*
document.write("<h1>This is a header</h1>");
document.write("<p>This is a paragraph</p>");
document.write("<p>This is another paragraph</p>");
*/
</script>

Try it yourself.

Using Comments at the End of a Line

In this example the comment is placed at the end of a line:

<script type="text/javascript">
document.write("Hello"); // This will write "Hello"
document.write("Dolly"); // This will write "Dolly"
</script>





Digg!Del.icio.us!Google!Live!Facebook!Slashdot!Technorati!StumbleUpon!Newsvine!Yahoo!Free social bookmarking plugins and extensions for Joomla! websites!
Last Updated ( Monday, 17 March 2008 )
 
< Prev   Next >

Polls

Which is the best Scripting language?