JavaScript comments
JavaScript comments can be used to improve the readability of your code.
JavaScript Comments
JavaScript will not execute comments.
We can add comments to explain JavaScript or improve the readability of the code.
Single-line comments begin with //.
This example uses a single line comment to explain the code:
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <h1 id="myH1"></h1> <p id="myP"></p> <script> // 输出标题: document.getElementById("myH1").innerHTML="Welcome to my Homepage"; // 输出段落: document.getElementById("myP").innerHTML="This is my first paragraph."; </script> <p><b>注释:</b>注释不会被执行。</p> </body> </html>
Run Example»
Click the "Run Example" button to view the online example
JavaScript multi-line comments
Multi-line comments start with /* and end with */.
The following example uses multi-line comments to explain the code:
Example
<!DOCTYPE html> <html> <head> <title>php中文网测试实例</title> <meta charset="utf-8"> </head> <body> <h1 id="myH1"></h1> <p id="myP"></p> <script> /* 下面的这些代码会输出 一个标题和一个段落 并将代表主页的开始 */ document.getElementById("myH1").innerHTML="欢迎来到php中文网"; document.getElementById("myP").innerHTML="这是一个段落。"; </script> <p><b>注释:</b>注释块不会被执行。</p> </body> </html>
Running Example»
Click the "Run Example" button to view the online example
Use comments to prevent execution
In the example below, comments are used to prevent the execution of one of the lines of code. (Can be used for debugging):
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <h1 id="myH1"></h1> <p id="myP"></p> <script> //document.getElementById("myH1").innerHTML="欢迎来到我的主页"; document.getElementById("myP").innerHTML="这是我的第一个段落。"; </script> <p><strong>注意:</strong> 注释块不会被执行</p> </body> </html>
Run Instance»
Click the "Run Instance" button to view the online instance
In the following example, comments are used to prevent the execution of a block of code (can be used for debugging):
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <h1 id="myH1"></h1> <p id="myP"></p> <script> /* document.getElementById("myH1").innerHTML="欢迎来到我的主页"; document.getElementById("myP").innerHTML="这是我的第一个段落。"; */ </script> <p><strong>注意:</strong>注释块不会被执行。</p> </body> </html>
Run Example»
Click the "Run Example" button to view the online example
Use comments
at the end of the line in the example below , we put the comment at the end of the line of code:
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p id="myP"></p> <script> var x=5; // 声明 x 并把 5 赋值给它 var y=x+2; // 声明 y 并把 x+2 赋值给它 document.getElementById("myP").innerHTML=y // 把 y 的值写到 myP </script> <p><b>注释:</b>注释不会被执行。</p> </body> </html>
Run Example»
Click "Run" Example" button to view online examples