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:

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>
      console.log("One");
      // console.log("Two");
      console.log("Three"); // console.log("Four");
      console.log("Five");
   </script>
   
</body>
</html>

The snapshot given below shows the sample output produced by above JavaScript example on comments:

javascript comment example

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>
      console.log("One");
      /*
      console.log("Two");
      console.log("Three");
      */
      console.log("Four"); /* console.log("Five"); */
      console.log("Six"); /* console.log("Seven");
      console.log("Eight"); */
      console.log("Nine");
   </script>
   
</body>
</html>

The output produced by this example is:

javascript multiline comment

JavaScript Online Test


« Previous Tutorial Next Tutorial »