Javascript Comments

Basically comments are used to include notes or suggestions or warnings to JavaScript code. This can make it easier to read and understand. Comments can also be used to disable code to prevent from execution.

JavaScript supports both C-style and C++-style comments:

  • Text written after // is treated as a comment and is ignored by JavaScript. This is used for a single line comment.
  • Text written between the characters /* and */ is treated as a comment. This may span multiple lines.
  • JavaScript also recognizes the HTML comment opening sequence is not recognized by JavaScript so it should be written as //-->.

Javascript Single Line Comments Example

<html>
<head>
<title>My first Javascript code</title>
</head>
// x = 5 + 6; I will not be executed
<body>
</body>
</html>

Javascript Multi Line Comments Example

<html>
<head>
<title>My first Javascript code</title>
</head>
//Below code will not be executed
/*
This function will used to
calculate add, sub, multiply and div operations. 
*/
function Calculate(a,b,type)
{
var ret_val;
	switch(type)
	{
		case "add":
		ret_val = a + b;
		break;
		case "sub":
		ret_val = a - b;
		break;
		case "mul":
		ret_val = a * b;
		break;case "div":
		ret_val = a / b;
		break;
	}
	return ret_val;
}
<body>
</body>
</html>