current position:Home>JavaScript 7. Comments

JavaScript 7. Comments

2022-09-23 09:20:06ghostwritten

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

copyright notice
author[ghostwritten],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/266/202209230915485660.html

Random recommended