Easy Tutorial
❮ Js Void Js Object Prototype ❯

JavaScript Comments


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


JavaScript Comments

JavaScript does not execute comments.

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

Single-line comments start with //.

This example uses a single-line comment 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.";

JavaScript Multi-line Comments

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

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

Example

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

Using Comments to Prevent Execution

In the following example, a comment is used to prevent the execution of a single line of code (useful for debugging):

Example

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

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

Example

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

Using Comments at the End of a Line

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

Example

var x = 5;    // Declare x and assign it the value 5
var y = x + 2;  // Declare y and assign it the value x + 2
❮ Js Void Js Object Prototype ❯