To write comments in Javascript, use the symbols // and /**/. Comments are useful to explain the workings of codes.
If you want to make a single-line comment, you have to put the symbol double slash(//) at the start of the line. A multi-line comment can be done by using the text inside the start symbol /* and the end symbol */. Let’s find out with the examples given below.
Single Line Comment in Javascript
If you want to add a single-line comment, you can use the symbol // at the start of the line, in which you want to comment.
Examples 1
XHTML
1
2
3
4
5
6
7
8
9
<script>
//This is the function myFun
functionmyFun(){
//document.write("This is the commented line.");
document.getElementById("mypara").innerHTML="This is the new paragraph.";//This is the simple comment
}
</script>
<p id="mypara">Click the below button to see the result.</p>
If you want to add multi-line comments in javascript, Use the symbol /* to the start of the comment and */ to the end of the comment. You can use the Javascript multiline comment system to make more than one-line comments.
Examples 2
XHTML
1
2
3
4
5
6
7
8
9
10
11
<script>
/*This is the example to show the javascript multiline comment system. The javascript example
does not display the comment line in the output.*/
functionmyMultiFun(){
/*document.write("This is the commented line.");
document.write("This is the second commented line.");*/
document.getElementById("myMultipara").innerHTML="This is the paragraph to show multiline comment.";
}
</script>
<p id="myMultipara">Click the below button to see the result.</p>