Latest

recent

JavaScript Comments


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

JavaScript Comments

Comments can be added to explain the JavaScript, or to make it more readable.
Single line comments start with //.

This example uses single line comments to explain the code:

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


JavaScript Multi-Line Comments

Multi line comments start with /* and end with */.
This example uses a multi line comment to explain the code:

<script type="text/javascript">
/*
The code below will write
one header and two paragraphs
*/
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>


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>

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>



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>
JavaScript Comments Reviewed by 1000sourcecodes on 07:42 Rating: 5
Powered by Blogger.