JavaScript Comments

comments in javascript

Not all JavaScript statements are “executed”. Code after a double slash //, or between /* and */, is treated as a comment. JavaScript Comments are ignored, and are not executed.


Advantages of JavaScript Comments

There are mainly two advantages of JavaScript comment.

  • To make code easy to understand It can be used to elaborate the code so that end user can easily understand the code.
  • To avoid the unnecessary code It can also be used to avoid the code being executed. Sometimes, we add the code to perform some action. But after sometime, there may be need to disable the code. In such case, it is better to use comments.

Types of JavaScript Comments

There are two types of comment in JavaScript.

  • Single-line Comment
  • Multi-line Comment

Single line Comment

Single line comments use double slashes.

<script>

// It is single line comment

document.write("hello Learner's");

</script>

Let’s see the example of single-line comment i.e. added after the statement.

<script>

var a=10;

var b=20;

var c=a+b;//It adds values of a and b variable

document.write(c);//prints sum of 10 and 20

</script>

Multi line Comment

Everything you write between /*and */ will be considered as a multi-line comment.

<script>

/* It is multi line comment.

It will not be displayed */

document.write("example of javascript multiline comment");

</script>



Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *