JavaScript Comments JAVASCRIPT

JavaScript Comments  

JavaScript Comments

JavaScript Comments

JavaScript comments can be used to explain JavaScript code, and to make it more readable.

Single-Line Comments

Single line comments start with //.

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

// Change heading:
document.getElementById("myPageHead").innerHTML = "My First Page";

// Change paragraph:
document.getElementById("myPara").innerHTML = "My first paragraph.";

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

var x = 3;      // Declare x, give it the value of 3
var y = x + 6;  // Declare y, give it the value of x + 6 

Example

// Change heading:
document.getElementById("myPageHead").innerHTML = "My First Page";

or

var x = 3;      // Declare x, give it the value of 3

Multi-line Comments

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

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

Example

/*
The code below will change
the heading with id = "myPageHead"
and the paragraph with id = "myPara"
*/
document.getElementById("myPageHead").innerHTML = "JavaScript Comments";
document.getElementById("myPara").innerHTML = "My first paragraph.";

Using Comments to Prevent Execution

Adding // in front of code, line changes the code lines from an executable line to comment.

Example

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Comments</h2>

<h1 id="myPageHead"></h1>

<p id="myPara"></p>

<script>
//document.getElementById("myPageHead").innerHTML = "JavaScript Comments";
document.getElementById("myPara").innerHTML = "My first paragraph.";
</script>

<p>The line starting with // is not executed.</p>

</body>
</html>

Download free E-book of JAVASCRIPT


#askProgrammers
Learn Programming for Free


Join Programmers Community on Telegram


Talk with Experienced Programmers


Just drop a message, we will solve your queries