Home » JavaScript » JavaScript comments

JavaScript Comments

Javascript comment is a line of text that is completely ignored by the JavaScript interpreter. They are used to provide extra information regarding the source code. It gives an overview about the code and helps in understanding the code better.People working on the same project can understand the code written by some other coder by reading his given comments.

It is used to add information about the code, warnings or suggestions so that end user or other programmers reading the code can easily interpret it./p>

Types of JavaScript Comments

There are 2 types of JavaScript Comments:
  • Single-line Comment
  • Multi-line Comment

JavaScript Single-line Comment

It is represented by double forward slashes //. Anything written after double forward slashes before line break will be taken as comments and will be ignored by the interpreter.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript Single Line Comments </title>
</head>
<body>
<script>
// It is single line comment
document.write("Hello javascript");
</script>
<p><strong>Note:</strong> The comments are not executed.</p>
</body>
</html>

Output

JavaScript Single Line Comments

Note: The comments are not executed.



<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript Single Line Comments </title>
</head>
<body>
<script>
var a=10; //Declare a, give it the value of 10
var b=20; // Declare b, give it the value of 20
var c=a+b; //It adds values of a and b variable
document.write(c); //prints sum of 10 and 20
</script>
<p><strong>Note:</strong> The comments are not executed.</p>
</body>
</html>

Output

JavaScript Single Line Comments

Note: The comments are not executed.


JavaScript Multi line Comment

This comment method can be used to add single as well as multi line comments. Multi-line comments start with /* and ends with */ and any text written between these, will be ignored by JavaScript.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript Multi Line Comments </title>
</head>
<body>
<script>
/* It is multi line comment.
It will not be displayed */
document.write("This is an example of javascript Multiline comment");
</script>
<p><strong>Note : </strong> The comment block is not executed. </p>
</body>
</html>

Output

JavaScript Multi Line Comments Examples

Note: The comment block is not executed.


Note: Comments can be used to ignore any line/block of code also, which you don't want to execute for a moment.











Follow Us: