How to Write Comments in Javascript
You can write comments in Javascript using the symbols // and /**/. If you want to make single line commented, you have to put the symbol double slash(//) at the start of the line. A multiline comment can be done by using the text inside the start symbol /* and end symbol */
Below are the different ways of writing comments in Javascript with examples.
Write Comments Using Javascript Single Line Comment
Syntax
1 |
// Put your commented text here |
You can use symbol // at the start of the line, which you want to make commented. It’s the same way of commenting out a line of code as you use in PHP.
See the example given below.
1 2 3 4 5 6 7 8 9 |
<script> //This is the function myFun function myFun(){ //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 below button to see result.</p> <button type="button" onclick="myFun()">Click Me</button> |
Output
Click below button to see result.
Write Comments Using Javascript Multi-Line Comment
You can use Javascript multiline comment system to make more than one line commented. Use the symbol /* to the start of the comment and */ to the end of the comment to create a multiline comment system in Javascript.
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 commente line in the output.*/ function myMultiFun(){ /*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 below button to see result.</p> <button type="button" onclick="myMultiFun()">Click Me</button> |
Output
Click below button to see result.
You must also read.