JavaScript comments
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 single-line comments to explain the code:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <h1 id="myH1"></h1> <p id="myP"></p> <script> // 輸出標(biāo)題: document.getElementById("myH1").innerHTML="Welcome to my Homepage"; // 輸出段落: document.getElementById("myP").innerHTML="This is my first paragraph."; </script> <p><b>注釋:</b>注釋不會被執(zhí)行。</p> </body> </html>
JavaScript multi-line comments
Multi-line comments start with /* Starts with */ and ends with */.
The following example uses multi-line comments to explain the code:
<!DOCTYPE html> <html> <head> <title></title> <meta charset="utf-8"> </head> <body> <h1 id="myH1"></h1> <p id="myP"></p> <script> /* 下面的這些代碼會輸出 一個標(biāo)題和一個段落 并將代表主頁的開始 */ document.getElementById("myH1").innerHTML="歡迎來到php中文網(wǎng)"; document.getElementById("myP").innerHTML="這是一個段落。"; </script> <p><b>注釋:</b>注釋塊不會被執(zhí)行。</p> </body> </html>
Use comments to prevent execution
In the following In the example, the comment is used to prevent the execution of one of the lines of code (can be used for debugging):
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <p>注釋</p> //這是注釋 </body> </html>
Use the comment
at the end of the line below In the example, we put the comment at the end of the line of code:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></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>注釋不會被執(zhí)行。</p> </body> </html>