Home >Web Front-end >JS Tutorial >【JavaScript Tutorial】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 start with //.
This example uses single-line comments to explain the code:
Example
// 输出标题: document.getElementById("myH1").innerHTML="欢迎来到我的主页"; // 输出段落: document.getElementById("myP").innerHTML="这是我的第一个段落。";
JavaScript multi-line comments
Multi-line comments start with /* and end with */.
The following example uses multi-line comments to explain the code:
Example
/* 下面的这些代码会输出 一个标题和一个段落 并将代表主页的开始 */ document.getElementById("myH1").innerHTML="欢迎来到我的主页"; document.getElementById("myP").innerHTML="这是我的第一个段落。";
Using comments to prevent execution
In the example below, the comment is used to prevent the execution of one of the lines of code (can be used for debugging):
Example
//document.getElementById("myH1").innerHTML="欢迎来到我的主页"; document.getElementById("myP").innerHTML="这是我的第一个段落。";
In the example below, comments are used to prevent the execution of a block of code (can be used for debugging):
Example
/* document.getElementById("myH1").innerHTML="欢迎来到我的主页"; document.getElementById("myP").innerHTML="这是我的第一个段落。"; */
Using comments at the end of lines
In the example below, we put comments at the end of the line of code Ending:
Example
var x=5; // 声明 x 并把 5 赋值给它 var y=x+2; // 声明 y 并把 x+2 赋值给它
The above is the content of JavaScript comments in [JavaScript Tutorial]. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!