HOME HTML CSS JAVASCRIPT SQL PHP JQUERY XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

JavaScript Comments


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


JavaScript Comments

Comments can be added to explain the JavaScript, or to make the code more readable.

Single line comments start with //.

Any text between // and the end of a line, will be ignored by JavaScript (will not be executed).

The following example uses single line comments to explain the code:

Example

// Write to a heading:
document.getElementById("myH1").innerHTML="Welcome to my Homepage";
// Write to a 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 */.

Any text between /* and */ will be ignored by JavaScript.

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

Example

/*
The code below will write
to a heading and to a paragraph,
and will represent the start of
my homepage:
*/
document.getElementById("myH1").innerHTML="Welcome to my Homepage";
document.getElementById("myP").innerHTML="This is my first paragraph.";

Try it yourself »


Using Comments to Prevent Execution

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

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 the comment is used to prevent the execution of multiple lines (can be suitable for testing):

Example

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

Try it yourself »


Using Comments at the End of a Line

In the following example the comments are placed 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 »



Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates

SHARE THIS PAGE