JavaScript 7. Comments

ghostwritten 2022-09-23 09:20:06 阅读数:36

javascriptcomments

JavaScript 7. Comments

insert image description here

1. JavaScript comments

JavaScript will not execute comments.

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

Single-line comments begin with // .

This example uses single-line comments to explain the code:

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

2. JavaScript multi-line comments

Multi-line comments start with /* and end with */.

The following example uses multi-line comments to explain the code:

/*The following code will outputa heading and a paragraphand will represent the start of the home page*/document.getElementById("myH1").innerHTML="Welcome to my homepage";document.getElementById("myP").innerHTML="This is my first paragraph.";

3. Use comments to block execution

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

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

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

4. Use a comment at the end of the line

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

var x=5; // declare x and assign 5 to itvar y=x+2; // declare y and assign x+2 to it
版权声明:本文为[ghostwritten]所创,转载请带上原文链接,感谢。 https://qdmana.com/2022/266/202209230915485660.html