WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JavaScript Comments


JavaScript comments can be used to make your code more readable.


Single Line Comments

JavaScript does not execute comments.

We can add comments to explain JavaScript or make the code more readable.

Single-line comments begin with // .

This example uses a single line of comment to explain the code:

Example

/Change heading:
document.getElementById("myH").innerHTML = "My First Page";
/Change paragraph:
document.getElementById("myP").innerHTML = "My first paragraph.";
Try it yourself »

Example

var x = 5;     /Declare x, give it the estimation of 5
var y = x + 2; /Declare y, give it the estimation of x + 2
Try it yourself »

Multi-line Comments

Multiline comments begin with / * and end with * / .

The following example uses multiple lines of comments to explain the code:

Example

/*
The code underneath will change
the heading with id = "myH"
what's more, the section with id = "myP"
in my web page:
*/
document.getElementById("myH").innerHTML = "My First Page";
document.getElementById("myP").innerHTML = "My first paragraph.";
Try it yourself »

Using Comments to Prevent Execution

In the following example, a comment is used to prevent execution of one of the lines of code (can be used for debugging):

Example

/document.getElementById("myH").innerHTML = "My First Page";
document.getElementById("myP").innerHTML = "My first paragraph.";
Try it yourself »
In the following example, comments are used to prevent the execution of a block of code (can be used for debugging):

Example

/*
document.getElementById("myH").innerHTML = "My First Page";
document.getElementById("myP").innerHTML = "My first paragraph.";
*/
Try it yourself »

Use comments at the end of the line

In the following example, we release the note to the end of the line of code:

Example

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