- 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, it can also be used to avoid the code being executed. Sometimes, we add the code to perform some action. But after some time, there may be a need to disable the code. In such case, it is better to use comments.
- Single-line Comment
- Multi-line Comment
Single line Comment
It is represented by double forward slashes (//). It can be used before and after the statement.
Let’s see the example of single-line comment which is added before the statement.
// It is single line comment
Let’s see the example of single-line comment which is added after the statement.
var c=a+b; //It adds values of a and b variable
document.write(c);//prints sum of 10 and 20
Multi line Comment
It can be used to add single as well as multi line comments. So, it is more convenient.
It is represented by forward slash with asterisk then asterisk with forward slash. For example:
/* your code here */
It can be used before, after and in the middle of the statement.
/* It is multi line comment.
It will not be displayed */