JavaScript Comments

The word comment refers to a written remark or opinion about any thing. And in computer programming world, a comment is a piece of text placed within a program, that helps other programmer to understand the program.

The comments are ignored while executing the program. Therefore, comments are often helpful while debugging the code.

You can use comment for multiple purposes. For example, use comment to:

  • explain some block of code
  • explain particular statement
  • avoid executing some statement or block of code
  • debug the code
  • etc.

Types of Comments in JavaScript

There are two types of comments, JavaScript allows. That are:

  1. Single line comment
  2. Multi-line comment

Single Line Comment in JavaScript

A single line comment in JavaScript, starts with //. That is, anything written after the // on the same line, will be treated as a comment. For example:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>
   
   <script>
      document.write("One");
      // document.write("Two");
      document.write("Three"); // document.write("Four");
      document.write("Five");
   </script>
   
</body>
</html>
Output

Multi-line Comment in JavaScript

Multi-line comment in JavaScript, starts with /* and ends with */. That is, anything between /* and */ will be treated as a comment. For example:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>
   
   <script>
      document.write("One");
      /*
      document.write("Two");
      document.write("Three");
      */
      document.write("Four"); /* document.write("Five"); */
      document.write("Six"); /* document.write("Seven");
      document.write("Eight"); */
      document.write("Nine");
   </script>
   
</body>
</html>
Output

JavaScript Online Test


« Previous Tutorial Next Tutorial »