Comments in JavaScript: Best Practices and Examples

Rumman Ansari   Software Engineer   2024-07-09 09:46:37   5556  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

JavaScript comments

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 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 comments in JavaScript.

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

1. JavaScript 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 i.e. added before the statement.

<span class="pln">
</span><span class="tag">&lt;script&gt;</span><span class="pln">  
</span><span class="com">// It is single line comment  </span><span class="pln">
document</span><span class="pun">.</span><span class="pln">write</span><span class="pun">(</span><span class="str">"hello javascript"</span><span class="pun">);</span><span class="pln">  
</span><span class="tag">&lt;/script&gt;</span><span class="pln"> 
</span> 

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

<span class="pln"> 
</span><span class="tag">&lt;script&gt;</span><span class="pln">  
</span><span class="kwd">var</span><span class="pln"> a</span><span class="pun">=</span><span class="lit">10</span><span class="pun">;</span><span class="pln">  
</span><span class="kwd">var</span><span class="pln"> b</span><span class="pun">=</span><span class="lit">20</span><span class="pun">;</span><span class="pln">  
</span><span class="kwd">var</span><span class="pln"> c</span><span class="pun">=</span><span class="pln">a</span><span class="pun">+</span><span class="pln">b</span><span class="pun">;</span><span class="com">//It adds values of a and b variable  </span><span class="pln">
document</span><span class="pun">.</span><span class="pln">write</span><span class="pun">(</span><span class="pln">c</span><span class="pun">);</span><span class="com">//prints sum of 10 and 20  </span><span class="pln">
</span><span class="tag">&lt;/script&gt;</span><span class="pln">  
</span>

2. JavaScript 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:

<span class="pln"> 
  </span><span class="com">/* 
    This is a multiline comment
     your can write comment  here
  */</span><span class="pln">  
</span>

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

 

<span class="pln"> 
</span><span class="tag">&lt;script&gt;</span><span class="pln">  
</span><span class="com">/* It is multi line comment.  
It will not be displayed */</span><span class="pln">  
document</span><span class="pun">.</span><span class="pln">write</span><span class="pun">(</span><span class="str">"example of javascript multiline comment"</span><span class="pun">);</span><span class="pln">  
</span><span class="tag">&lt;/script&gt;</span><span class="pln">  
</span>


No Questions Data Available.
No Program Data.

Stay Ahead of the Curve! Check out these trending topics and sharpen your skills.