codescracker


javascript

JavaScript Comments



« Previous Tutorial Next Tutorial »


JavaScript comments is a line of text that is not executed. You can use comment in JavaScript to explain JavaScript code to make the code more readable.

Comments sometime also helps in debugging the code. There are two types of comments in JavaScript which are:

JavaScript Single Line Comments

Every text after // is single line comment. In other word, single line comment starts with //. Here is an example, uses single line comment

<!DOCTYPE html>
<html>
<head>
	<title>JavaScript Single Line Comment Example</title>
</head>
<body>

<p id="comment_para11">I will be change</p>
<p id="comment_para12">I will also be change</p>
<script>
	// Change paragraph 1 :
	document.getElementById("comment_para11").innerHTML = "This is JavaScript Comment Tutorial.";
	// Change paragraph 2 :
	document.getElementById("comment_para12").innerHTML = "This is Single-Line Comment Example.";
</script>

</body>
</html>

Here is the output produced by the above JavaScript single-line comment example:

javascript comment

Here is the live demo output of the above single-line comment example in JavaScript:

I will be change

I will also be change

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

<!DOCTYPE html>
<html>
<head>
	<title>JavaScript Single Line Comment Example</title>
</head>
<body>

<p id="comment_para2"></p>
<script>
	var num1 = 50;      // declares a variable named num1 and initializes a value 50 to it
	var res = num1 + 60;  // declares a variable named res and initializes a value 60 + num1(50) to it
	
	document.getElementById("comment_para2").innerHTML = res; // now writes the value of res to the paragraph with id comment_para2
</script>

</body>
</html>

Here is the sample output produced by the above single-line comment program in JavaScript:

javascript comment example

JavaScript Multi-line Comments

Every text starts with /* and ends with */, are multi-line comments, no matter how many lines taken. In other words, multi-line comments in JavaScript starts with /* and ends with */. Here is an example:

<!DOCTYPE html>
<html>
<head>
	<title>JavaScript Multiline Comment Example</title>
</head>
<body>

<p id="comment_para3">I am a paragraph</p>
<div id="comment_div3">I am a div</div>
<p>
	<input type="button" onclick="comment_fun3()" value="Click Here"/>
</p>
<script>
	/*
	The code given below will change
	the paragraph having id, "comment_para1",
	and the div having id, "comment_div3"
	*/
	function comment_fun3()
	{
		document.getElementById("comment_para3").innerHTML = "Hello Aditya";
		document.getElementById("comment_div3").innerHTML = "How are you?";
	}
</script>

</body>
</html>

Here is the output produced by the above multiline comment example in JavaScript. This is the initial output:

javascript single line comment

Now click on the Click Here button, you will watch the output as shown in the snapshot given below:

javascript multiline comment

Here is the live demo output of the above multiline comment in JavaScript.

I am a paragraph

I am a div


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test