JavaScript Comments


JavaScript comments can be used to improve code readability.


JavaScript Comments

JavaScript will not execute comments.

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

Single-line comments start with//at the beginning.

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

Example

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

Try it yourself »


JavaScript Multi-line Comments

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

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

Example

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

Try it yourself »


Use Comments to Prevent Execution

In the following example, comments are used to prevent the execution of one of the code lines (useful for debugging):

Example

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

Try it yourself »

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

Example

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

Try it yourself »


Use Comments at the End of a Line

In the following example, we place the comments at the end of the code lines:

Example

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

Try it yourself »
Other Extensions