Home >Web Front-end >JS Tutorial >How to comment code in javascript

How to comment code in javascript

青灯夜游
青灯夜游Original
2021-04-12 17:40:094365browse

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*/".

How to comment code in javascript

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.

JavaScript single-line comments

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:

##JavaScript multi-line comment

Multi-line comments start with

/* and end 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>
/*
下面的这些代码会输出
一个标题和一个段落
并将代表主页的开始
*/
document.getElementById("myH1").innerHTML="欢迎来到石海莹的博客";
document.getElementById("myP").innerHTML="这是一个欢迎的段落。";
</script>
<p><b>注释:</b>注释块不会被执行。</p>

</body>
</html>

The page output is:

Use comments to Prevent execution

⑴Example 1: 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>注释组织执行</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>

The page output result is:

⑵Example 2: In the following example, comments are used to prevent the execution of code blocks (can be used for debugging):

76c82f278ac045591c9159d381de2c57
100db36a723c770d327fc0aef2ce13b1
93f0f5c25f18dab9d176bd4f6de5d30e 
4d0d87937f6c83b675e896c64d3eb7c9 
b2386ffb911b14667cb8f0f91ea547a7注释块不执行6e916e0f7d1e588d4f442bf645aedb2f 
9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d

cd51d6d08de83f962afff888184c4840473f0a7621bec819994bb5020d29372a
66825e7d7975fe099d6e9cfefe46a82a94b3e26ee717c64999d7867364b1b4a3
3f1c4e4b6b16bbbd69b2ee476dc4f83a
/*
document.getElementById("myH1").innerHTML="欢迎来到我的主页";
document.getElementById("myP").innerHTML="这是我的第一个段落。";
*/
2cacc6d41bbb37262a98f745aa00fbf0
e388a4556c0f65e1904146cc1a846bee8e99a69fbe029cd4e2b854e244eab143注意:128dba7a3a77be0113eb0bea6ea0a5d0注释块不会被执行。94b3e26ee717c64999d7867364b1b4a3

36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e
The page output is:

Use comments at the end of lines

In the following example, put the comment at the end of the line of code:

var x=5;    // 声明 x 并把 5 赋值给它
var y=x+2;  // 声明 y 并把 x+2 赋值给它

The importance of comments

Develop a good habit of writing comments.

Convenient for you to read again and maintain your code.

If the project changes hands later, it will also make it easier for others to understand and maintain your code.

[Recommended learning:

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn