JavaScript comments

JavaScript will not execute comments.

We can add comments to explain JavaScript or improve the readability of the code.

JavaScript comments

Comments refer to the An additional comment added to a program file or code snippet. Comments can improve code readability, allowing yourself or other developers to understand the program more quickly.

JavaScript comments are not part of the JavaScript program, and their content does not participate in any functional calculations in the program. They are displayed in a special color (such as green) in the editor.

JavaScript comment symbols

JavaScript comments are divided into single-line comments and multi-line comments.

JavaScript single-line comment

Single-line comment means only commenting on one line. The comment symbol is //. What follows this symbol is the content of the comment until the end of the line. The following is an example of an annotation:

// The following is a pop-up prompt message box
alert("I am the prompt text");

##JavaScript Multi-line comments

Multi-line comments can comment on multiple lines of code at a time. The multi-line comment symbols start with /* and end with */. The following is an example of a multi-line comment:

/*The following is a pop-up prompt message box
In this line of code, there is no need for any variables or parameters
*/
alert("I am the prompt text ");

The comment content is not only some explanatory text, but also some temporarily unused code fragments, etc., to prohibit the execution of these codes.

Obviously, it is more convenient to use the double slash single-line comment method for a small number of comments that span one or several lines. For large numbers of comments, especially large sections of code that are not needed temporarily, it is easier to use multi-line comments.

<!DOCTYPE html>
<html>
<head>
<title>php中文网测试实例</title>
<meta charset="utf-8">
</head>
<body>
    <h1 id="myH1"></h1>
    <p id="myP"></p>
    <script>
    /*
    下面的这些代码会输出
    一个标题和一个段落
    并将代表主页的开始
    */
    document.getElementById("myH1").innerHTML="php中文网";
    document.getElementById("myP").innerHTML="这是一个注释的演示。";
    </script>
</body>
</html>

Use comments to prevent 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):

//document.getElementById("myH1").innerHTML="My homepage";
document.getElementById("myP").innerHTML="My paragraph.";

In the following example, comments are used to prevent the execution of a block of code (can be used for debugging):

/*
document.getElementById("myH1").innerHTML= "Welcome to my homepage";
document.getElementById("myP").innerHTML="This is my first paragraph.";
*/

Use comments at the end of the line

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

var x=5; / / Declare x and assign 5 to it
var y=x+2; // Declare y and assign x+2 to it

Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <script type="text/javascript"> document.write("<h3>这是标题1</h3>"); /* document.write("<h1>这是标题2</h1>"); document.write("<p>这是段落。</p>"); document.write("<p>这是另一个段落。</p>"); */ </script> </head> <body> <h1></h1> <p></p> </body> </html>
submitReset Code