Loading
Comments in JS-tutorial
In JavaScript, comments help developers explain, document, and make code easier to read - both for themselves and for others who may work on the same project.

Comments do not affect the actual code execution - they are completely ignored by the JavaScript interpreter.



There are two main types of comments in JavaScript


1. Single-line Comment

Used to write brief comments on one line.


Syntax:

// This is a single-line comment



Example:

<script>  
// It is single line comment  
document.write("Welcome to Quipoin");  
</script>  



Example: with code explanation

<html>

<body>
  <script>
    var a = 10;
    var b = 10;
    var c = a + b; // It adds values of a and b
    document.write(c); // prints sum of 10 and 10
  </script>
</body>

</html>

Output

Uploaded Image




2. Multi-line Comment

Used when you want to write longer notes or comment out multiple lines.


Syntax:

    /*
 This is a multi-line comment.
 It can span multiple lines.
*/



Example: 

<html>

<body>
  <script>
    /* It is multi line comment.  
    This comment won’t appear on the webpage */
    document.write("Welcome to Quipoin");
  </script>
</body>

</html>

Output

Uploaded Image




Why use comments in JavaScript ?

  • To explain what the code does for future reference.
  • To temporarily disable code during testing.
  • To make collaboration with other developers easier.