How to comment code in JavaScript: 1. Use single-line comments with the syntax "//code that needs to be commented"; 2. Use multi-line comments with "/*" and "*/" for the code that needs to be commented. Surrounding, syntax "/*comment content*/".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
JavaScript will not execute comments.
We can add comments to explain JavaScript or improve the readability of the code.
Single-line comments begin with //
.
Example introduction:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>单行注释</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>
The page output result is:
/* and end with
*/.
<!DOCTYPE html> <html> <head> <title>多行注释</title> <meta charset="utf-8"> </head> <body> <h1 id="myH1"></h1> <p id="myP"></p> <script> /* 下面的这些代码会输出 一个标题和一个段落 并将代表主页的开始 */ document.getElementById("myH1").innerHTML="欢迎来到石海莹的博客"; document.getElementById("myP").innerHTML="这是一个欢迎的段落。"; </script> <p><b>注释:</b>注释块不会被执行。</p> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>注释组织执行</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>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>注释块不执行</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>
var x=5; // 声明 x 并把 5 赋值给它 var y=x+2; // 声明 y 并把 x+2 赋值给它
javascript advanced tutorial]
The above is the detailed content of How to comment code in javascript. For more information, please follow other related articles on the PHP Chinese website!