Javascript Comments

The Javascript comment are the meaningful way to deliver message. It is used to explain Javascript code, any warnings or suggestions so that end user can easily understand the code.

These comments are ignored by the Javascript engine i.e embedded in the browser and are used to prevent execution of code is suitable for code testing.


Advantages of Javascript Comments

Following are the advantages of using javascript comments within our code:-

  • To make code easy to understand:- It can be used to elaborate the code so that end user can easily understand the code.
  • To avoid the unnecessary code:-  Sometimes, we add the code to perform some action. But after sometime, there may be need to disable the code. In such case, it is better to use comments.

Types of Javascript Comments

There are two types of comments available in javascript:-

  • Single line comment:- These comments are written in one line represented by double forward slashes (//). It can be used before and after the statement.
<script>  
   // It is single line comment  
   document.write("hello");  
</script>  
  • Multi line comment:- These comments can be written in multiple lines represented by forward slash and backward slash with asterisk(/* */).

It can be used before, after and middle of statement.

<script>  
   /* It is multi line comment.  
   It will not be displayed */  
   document.write("hello");  
</script>  

Note:- It is most common to use single line comments. Block comments are often used for formal documentation.