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.
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.
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:
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.
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: