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 //
:
<script type="text/javascript">
// This will write a header:
document.write( "<h1>This is a header</h1>" );
// This will write one paragraph:
document.write( "<p>This is a paragraph</p>" );
</script>
|
JavaScript Multi-Line Comments
Multi line comments start with
/*
and end with
*/
:
<script type="text/javascript">
/* The code below will write
one header and one paragraph. */
document.write( "<h1>This is a header</h1>" );
document.write( "<p>This is a paragraph</p>" );
</script>
|
Using Comments to Prevent Execution
<script type="text/javascript">
// document.write( "<h1>This is a header</h1>" );
// document.write( "<p>This is a paragraph</p>" );
</script>
|
Using Comments at the End of a Line
<script type="text/javascript">
document.write( "Hello" ); // This writes "Hello."
document.write( "Dolly" ); // This writes "Dolly."
</script>
|
Demonstration
The following demonstration shows how the script of HTML and JavaScript is displayed on the Web.