From http://www.w3schools.com (Copyright Refsnes Data)

JavaScript Comments

previous next

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>

Try it yourself.


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>

Try it yourself.


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>

Try it yourself.

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>

Try it yourself.


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>


previous next

From http://www.w3schools.com (Copyright Refsnes Data)