javascript 注释
Like other programming languages, in JavaScript comments can be used to prevent the execution of the statements. Whenever we want to ignore the statements or any text/document from execution, we use comments.
像其他编程语言一样, JavaScript注释可用于阻止执行语句。 每当我们想要忽略语句或执行中的任何文本/文档时,我们都会使用注释。
For example: While writing the code, sometimes the logic is complex and we want to explain in our words so that we can understand the logic – we can put the explanation in the comments.
例如:在编写代码时,有时逻辑很复杂,我们想用文字解释,以便我们可以理解逻辑–可以将解释放在注释中。
There are two types of comments in JavaScript:
JavaScript有两种类型的注释 :
Single line comment
单行注释
Multi line comments
多行注释
JavaScript单行注释 (JavaScript single line comment)
For single line comment in JavaScript, we use double slash characters (//) before the text/code. The code/text written after the double slash (//) will be ignored by the interpreter.
对于JavaScript中的单行注释,我们在文本/代码之前使用双斜杠(//)。 解释器将忽略双斜杠(//)之后编写的代码/文本。
Syntax:
句法:
//This text will not be executed/ interpreted
Example:
例:
<script>
document.write("This is line1<br>");
//document.write("This is line2<br>");
document.write("This is line3<br>");
//document.write("This is line4<br>");
document.write("This is line5<br>");
</script>
Output
输出量
This is line1
This is line3
This is line5
JavaScript多行注释 (JavaScript multi line comment)
Multi-line comments can be placed between /* and */. The comment starts with /* and terminates with */. We can place any number of lines between these comment characters.
多行注释可以放在/ *和* /之间。 注释以/ *开头,以* /结束。 我们可以在这些注释字符之间放置任意数量的行。
Syntax:
句法:
/*
This line will not be executed/ interpreted
This line will also not be executed/ interpreted
This line will also not be executed/ interpreted
*/
Example:
例:
<script>
document.write("This is line1<br>");
/*document.write("This is line2<br>");
document.write("This is line3<br>");*/
document.write("This is line4<br>");
/*
document.write("This is line5<br>");
document.write("This is line6<br>");
document.write("This is line7<br>");
*/
document.write("This is line8<br>");
document.write("This is line9<br>");
</script>
Output
输出量
This is line1
This is line4
This is line8
This is line9
在语句中注释代码 (Comment the code in a statement)
We can comment on any code in a statement by using multi-line characters.
我们可以使用多行字符对语句中的任何代码进行注释。
Example:
例:
<script>
document.write("This is line1<br>");
document.write("This is line2<br>");
document.write(/*"This is line3<br>"*/"Hello world<br>");
var result = /*a-b*/ 10-2;
document.write("result: " + result + "<br>");
</script>
Output
输出量
This is line1
This is line2
Hello world
result: 8
翻译自: https://www.includehelp.com/code-snippets/comments-in-javascript.aspx
javascript 注释