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:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <h1 id="myH1"></h1> <p id="myP"></p> <script> // 輸出標(biāo)題: document.getElementById("myH1").innerHTML="PHP中文網(wǎng)"; // 輸出段落: document.getElementById("myP").innerHTML="PHP.CN"; </script> <p><b>注釋:</b>注釋不會被執(zhí)行。</p> </body> </html>
Run the program and try it
JavaScript multi-line comments
Multi-line comments start with /* and end with */.
The following example uses multi-line comments to explain the code:
<!DOCTYPE html> <html> <head> <title>php中文網(wǎng)測試實例</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>
Run the program to try it
Use comments to block execution
In the following 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>php中文網(wǎng)(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> 注釋塊不會被執(zhí)行</p> </body> </html>
Run the program to try it
Note : Multi-line comments are used the same as above. Just wrap the code blocks that do not need to be executed with multi-line comments.
Use comments at the end of the line
Like this
var x=5; // Declare x and assign 5 to it