Comments in JavaScript
Oct 19, 2020 09:45 0 Comments Javascript PARTH

                   Comments in JavaScript

 

The JavaScript comments are meaningful way to deliver message. It is used to add information about the code, warnings or suggestions so that end user can easily interpret the code.

The JavaScript comment is ignored by the JavaScript engine i.e. embedded in the browser.

Advantages of JavaScript comments

There are mainly two advantages of JavaScript comments.

  1. To make code easy to understand It can be used to elaborate the code so that end user can easily understand the code.
  2. 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 some time, there may be a need to disable the code. In such case, it is better to use comments.

Types of JavaScript Comments

There are two types of comments in JavaScript.

  1. Single-line Comment
  2. Multi-line Comment

Single line Comment

It is represented by double forward slashes (//). It can be used before and after the statement.

Let’s see the example of single-line comment which is added before the statement.

<script>  

// It is single line comment  

document.write("hello javascript");  

script>  

Let’s see the example of single-line comment which is 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

It can be used to add single as well as multi line comments. So, it is more convenient.

It is represented by forward slash with asterisk then asterisk with forward slash. For example:

/* your code here  */  

It can be used before, after and in the middle of the statement.

<script>  

/* It is multi line comment.  

It will not be displayed */  

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

script>  

 

Prev Next
About the Author
Topic Replies (0)
Leave a Reply
Guest User

You might also like

Not sure what course is right for you?

Choose the right course for you.
Get the help of our experts and find a course that best suits your needs.


Let`s Connect