User Rating: 1 / 5

Star ActiveStar InactiveStar InactiveStar InactiveStar Inactive
 

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 someone else views your code, they will also be able to understand the code.

Another great thing about comments is the ability to 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. When you place these two slashes, all text to the right of them will be ignored, until the next line.

Example:

 <html>
<body>
<script>
//This is a Single line comment
document.write("<h1>Mindphp.com</h1>");
//This is another Single line comment
</script>
</body>
</html>

This is the result:

 

Creating Multi-line comments:

 Although a single line comment is quite useful, it can sometimes be burdensome to use when disabling long segments of code or inserting long-winded comment. For this large comments you can use JavaScript's multi-line comment that begin with /* and end with */

Example:

<html>
<body>
<script>
/*This is a comment
This is a comment
This is a comment
*/
document.write("<h1>JavaScript</h1>");
</script>
</body>
</html>

This is the result:

 

 

Next

Chapter 6 - JavaScript Variables