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.

   php中文网测试实例  

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
||
php中文网(php.cn)

submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!