If you want to be a good scripter/programmer, it is highly suggested that you seriously consider adding comments within your code. For one, having comments will make it much easier for you or someone else to re-read your code. The more time that passes from when you wrote the original code to the time you come back to revisit it, the more valuable those comments become.
The HTML and JavaScript comments are used in the same block but serve different purposes.
The HTML comment is used to prevent the JavaScript code appear as plain text for those users that are running an older browser version that does not support JavaScript.
The JavaScript comments are used to add a notation to your JavaScript code or prevent a JavaScript statement from executing, during the debugging process.
Single-line Comments
Single-line comments start with //
.
The following example shows the use of a single-line comment.
<script type="text/JavaScript">
<!--
// This is an alert
alert("Alert #1");
// This is another alert
alert("Alert #2");
//-->
</script>
Multi-line Comments
There may be instances where you need to add multiple-line comments to your scripts. While you could simply add double slashes at the beginning of each line, it is not as convenient. This is where the multi-line comment tags may be used.
Multi-line comments consist of the opening comment tag /*
, the comments themselves, followed by the closing comment tag */
.
Here is an example of a multi-line comment:
<script type="text/JavaScript">
<!--
/* The following two functions will result in
your browser displaying two pop-up windows.
After you click OK on the first box, the second
pop-up window will appear. */
alert("Alert #1");
alert("Alert #2");
//-->
</script>
While it may seem like a tedious task, take the time to add meaningful comments. It not only makes your JavaScript easier to read but also helps you troubleshoot bugs in the script.