JavaScript – Comments


What is a JavaScript comment ?

JavaScript comments are the only meaningful way to deliver message inside the code.

JavaScript comments is a useful notation to explain what the program does.

JavaScript comments explain the code flow and makes the program more readable for the programmers.

Javascript code written inside the comments is not processed by the interpreter.

What is a JavaScript interpreter ?

Interpreter is a computer program also a JavaScript engine which executes JavaScript code.

JavaScript now uses the Just-in-compilation process for increase its performance.

In modern browsers, the JavaScript engine runs the code thereby rendering output via the Document Object Model (DOM).

What are the advantages of JavaScript comments ?

JavaScript comments are used to add information about the code, warnings or suggestions so that the programmer can easily understand the code.

JavaScript comments can also be used to prevent some code lines from being executed.

JavaScript comments are used to comment out some code for debugging the errors while execution.Using Comments to Prevent Execution

Become a Javascript full stack developer !!!

Click to Learn More about – Javascript online learning

Example :

<html>
	<head>
		<title>Our title</title>
		<script>
			// var a = document.getElementById("hId");
			   var b = document.getElementById("pId");
			// a.innerHTML = "Javascript heading element";
			   b.innerHTML = "Javascript paragraph element";
		</script>
	</head>
	<body>
		<h1 id="hId">Heading 1</h1>
		<p id="pId">This is Paragraph...</p>
	</body>
</html>

Output :


Since ( var a ) related codes are commented, the program skips those steps for execution the produces the output as,

Javascript paragraph element

Thus JavaScript code is used to prevent some code lines from being executed.

What are the types of comments ?

There are two types of comments in JavaScript.

1. Single-line comment.

2. Multi-line comment.What is a single line comment ?

What is a single line comment ?

Example :

<html>
	<head>
		<title>Our title</title>
		<script>
			var a = 20;
			var b = 10;
			var c = a+b;
		//	var d = a-b; commenting variable d
			document.write("The sum value c = " + c);
		//	document.write("The minus value d = " + d);
			
		</script>
	</head>
	<body></body>
</html>

Single-line comment is used to prevent (var d ) related code from being executed, and the output will be

Output :

The sum value c = 30

Example :

<html>
        <head>
                <title>Our title</title>
                <script>
                        var a = 20;
                        var b = 10;
                        var c = a+b; // sum value of c 
                        var d = a-b; // minus value of d
                        document.write("The sum value c = " + c);
                        document.write("The minus value d = " + d);

                </script>
        </head>
        <body></body>
</html>

Single-line comments is used to explain the code variables c and d.

Output :

The sum value c = 30

The minus value d = 10

What is multi-line comment ?

Javascript Multi-line comments is used to comment on bigger parts (a few lines) of code.

It needs a proper starting point and closing part.

It can be used before, after and middle of the javascript statements.

Syntax :


/* 
	The commented code 
	This can be of multi-line
*/

Multi-line code prevents multiple lines of code being executed.example code :

Example :

<html>
        <head>
                <title>Our title</title>
                <script>
              /*        var x = 20;
                        var y = 10;
                        var z = x+y; // sum value of z
                        document.write("The sum value z = " + z);
		*/
			var a = 15;
			var b = 5
                        var d = a-b; // minus value of d
                        document.write("The minus value d = " + d);
                </script>
        </head>
        <body></body>
</html>

Multi-line code prevents some portion of code related to (x,y,z) from execution and executes (a,b,c) related code, and the output will be

Output :

The minus value d = 10

Multi-line code explaining the code flow.example code :

Example :

<html>
        <head>
                <title>Our title</title>
                <script>
              /*   
			This program explains the subtraction value.
			In this variables a and b are declared
			The subtraction value is calculated and displayed in variable d
                */
                        var a = 15;
                        var b = 5
                        var d = a-b; // minus value of d
                        document.write("The minus value d = " + d);
                </script>
        </head>
        <body></body>
</html>

Output :

The minus value d = 10