Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

JavaScript comments


May 06, 2021 JavaScript


Table of contents


JavaScript comments


JavaScript comments can be used to improve the readability of your code.


JavaScript comments

JavaScript does not perform comments.

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

A single line of comments begins with //.

This example uses a single-line comment to interpret the code:

// Output title:
Document.getElementByid ("myh1"). Innerhtml = "Welcome to my home page";
// Output paragraph:
Document.getElementByid ("myp"). Innerhtml = "This is my first paragraph.";

Try it out . . .


JavaScript multi-line comments

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

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

/*
The following code will output
One title and a paragraph
And will represent the beginning of the home page
*/
Document.getElementByid ("myh1"). Innerhtml = "Welcome to my home page";
Document.getElementByid ("myp"). Innerhtml = "This is my first paragraph.";

Try it out . . .


Use comments to block execution

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

//document.getlementByid ("Physih1" ).innerhtml=" Welcome to My Home ";
Document.getElementByid ("myp"). Innerhtml = "This is my first paragraph.";

Try it out . . .

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

/*
Document.getElementByid ("myh1"). Innerhtml = "Welcome to my home page";
Document.getElementByid ("myp"). Innerhtml = "This is my first paragraph.";
*/

Try it out . . .


Use comments at the end of the line

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

VAR x = 5; // declare X and assign 5 assignment
VAR Y = X + 2; // Declaration Y and assign X + 2 to it

Try it out . . .

Related exercises

JavaScript Programming Live: JavaScript Comment Statement Action