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

How to comment code in javascript

青灯夜游
Release: 2023-01-05 16:07:42
Original
4308 people have browsed it

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>
Copy after login

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>
Copy after login

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>
Copy after login

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):

<!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>
Copy after login

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 赋值给它
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template