JavaScript Comments or ( JS Comments ) are very useful and practical when developing code. It is possible to comment out a specific line or a block of code with JavaScript Comments.
You may need to make comments on code, such as to describe what a code is supposed to do. It’s also possible that you will want to disable a line of the script for some reason in webpage. For instance, if you are looking for an error in a script.
You can accomplish these tasks in Netscape JavaScript by using JavaScript comments. You can insert JavaScript comments that appear on one line Comment or run for numerous lines comments.
Fortunately, most programming languages provide a way for programmers to leave comments for themselves or other programmers who might look through their Script. A JavaScript comments is simply a line or more worth of comments: the Netscape JavaScript interpreter ignores them.
We’ll be covering the following topics in this tutorial:
Creating Single Line Comments
If you want to add comment on a single line in your script, place a pair of forward slashes before the text of the comment:
// Your Code is here
In this format, anything preceding the two slashes on that line is “live” Java Script webpage that will be executed and anything after the slashes on that line is ignored. For example, suppose that you wrote this line in your comments script:
document.write("This is cool! web page"); // Comment writes out Here
The document.write() method will be run by the web browser, so the text “This is cool! web page” will be written to the web page. However, the Java Script comment after the slashes will be ignored by the web browser. If you place the forward slashes at the beginning of a comment, the browser will ignore the entire line. Suppose that you move the slashes in the previous example to be the first items on the line:
// document.write("This is cool! web page"); Comment writes out Here
In this format, the entire line is ignored by Java Script, since it begins with the two slashes that represent a Java Script comment. The text will not be written to the web page, since the script will not be executed by the web browser. In effect, you are disabling the document.write() statement. You may wish to do this if the Java Script containing this line has an error and you want to know whether or not this line is causing the problem in script.
Multiple-Line Comments
Java Script Comments denoted by a pair of forward slashes apply only to the line on which they appear. To add Java Script comments that span any number of lines in script, you use a different comment format: a forward slash followed by an asterisk at the beginning of the comment, then the text of the comment, and then an asterisk followed by a forward slash at the end of the comment. Here’s an example:
/* My Comments Code will write here All of this JavaScript Comments text is ignored by the web browser. */ document.write ("You can see Comment ");
Using this format, you can begin the comment in one line and end it on another line. Multiple-line comments can be handy when you want to insert lengthier descriptions. Look at this Multiline Comments example here.
<script type="text/JavaScript">
/*
This Multiline Comments code won’t work for some reason.
document.write ("Multiline Comments code");
</script>
Did you notice that the closing comment symbols are missing? In above example, the comment just keeps going on with no end in sight. To fix this Comments, you need to close the comments before the document.write () method is used:
<script type="text/javascript"> /* The comment code is now working! This text is hidden. You need to close the comments before the document.write () method */ document.write("Now everyone can see Comments code ! "); </script>