JS Comments

JS comments could be used to explain JS code, and to produce more readable code.

JS comments could also be used to prevent execution of statements, when testing alternative code.

Different types of JS comments are shown below.

 

1. JS Single Line Comment:

Comments can be added to explain the JS, or to make it more readable. Single line comments start with //.  This example uses single line comments to explain the code:

<html>
<body>
<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>
</body>
</html>
 

2. JS Multi-Line Comments:

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

<html>
<body>
<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>
</body>
</html>
 

3. JS Comments at the End of a Line:

In this example the comment is placed at the end of a line:

<html>
<body>
<script type="text/javascript">
document.write("Hello"); // This will write "Hello"
document.write("Jolly"); // This will write "Jolly" </script>
</body>
</html>