Menu Close

JavaScript Comments

JavaScript Comment

In this JavaScript tutorial, we are going to learn JavaScript comment. Comment in JavaScript explains the code. A comment is the best way to explain the JavaScript code.JavaScript support single and multiline Comment.

In the previous tutorial, we have seen JavaScript syntax to understand the JavaScript code structure.

What is JavaScript Comment?

JavaScript Comment is used to explain the JavaScript code and make code more readable for developers.
JavaScript code can also be used to prevent code execution When testing the alternate code. JavaScript supports both multiline and single line Comment.

Why use Commnets in JavaScript?

Comments in JavaScript are played a more important role inside, Because using Comments, developers can explain their JavaScript code so that code becomes more understandable by other developers.
Sometimes we don’t want to execute any specific part of the JavaScript code, Then we can Comment on that particular code instead of deleting.
JavaScript provides two ways to define Comment inside JavaScript code: Single line Comment and Multiline Comment.

JavaScript Single Line Comment?

JavaScript Single lne Comment start with double slash ( // ).
If any text written inside between //, is treated Comment and it will be completely ignored by JavaScript.

Example

In this example we used single line comment.


<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>JavaScript Comment</h2>
<p id="myID1"></p>

<script>

//This is the single-line comment.
var x, result;
x = 100;
result = x * 10;
document.getElementById("myID1").innerHTML = result;

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

JavaScript Multiline Comment:

In JavaScript, the Multiline comment starts and ends with /* and */. Any text written between /* and */ will be ignored by the JavaScript.

Example


<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>JavaScript Multiline Comment</h2>
<p id="myID1"></p>

<script>

/* Here we declare two variables x and result and assign 100 to the variable x and perform multiplication using * operator and store the output in the result variable. */

var x, result;
x = 100;
result = x * 10;
document.getElementById("myID1").innerHTML = result;

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

Prevent the Execution:

Using JavaScript Comment we can prevent the JavaScript execution. Sometimes we don’t want to execute any specific part of the JavaScript program, Then we can use comments to prevent the execution.

Example


<!DOCTYPE html>
<html>

<head>
</head>

<body>
    <h2>JavaScript Multiline Comment</h2>
    <p id="myID1"></p>

    <script>
        /* Here we declare two variable x and result and assign 100 to the variable x and perform multiplication using * operator and store the output in the result variable. */

        var x, result;
        x = 100;
        result = x * 10;
        //alert(result);
        document.getElementById("myID1").innerHTML = result;
    </script>
</body>
</html>

Conclusion:

In this article, you have learned JavaScript Comment with an example. I recommend you, When you have large code Then you explain the code through Comment so that code can be more readable by other developers as well as developers.

I hope this article will help you. if you like this article, please share it with your friends who want to learn JavaScript.

Learn about the comment:- Click Here
For more information:- Click Here

JavaScript Syntax
JavaScript Variables

Related Posts