- 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.
- 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.