Register Login

Commenting in JS

The primary purpose of code is to make the computer understand what we want to develop. However, it is also essential for a program to keep a section or any technique that explicitly helps interpret the JavaScript code easily and meaningfully wherever possible. That is where comments come into action. In this article, you will learn about the commenting technique of JavaScript along with its types.

What are Comments?

Every programming language offers us to leave notes for our fellow developers. Comments are programming concepts in JavaScript that allow writing notes within the source code. The JavaScript engine ignores such codes. It means the comment won't affect the output anyway. Comments are mostly meant for documentation of a function or block of code.

Different types of comments:

Since by now we have got to know the magical usages of a comment code, let us now dive into its types:

There are two types of comments:

  • Single line comment
  • Multi-line comment

Single line comment:

Codes written within the JavaScript comments are not interpreted by the interpreter. They are straightway ignored when the interpreter found the double slash before any code snippet. Single line comment works for a single line of the code. It starts with a “//”. Any line of code written after the symbol will not get executed as a token and will be ignored entirely.

Syntax:

// everything written after double slash becomes a comment

Example:

// a single line comment
console.log("JavaScript comments");

Multi-line comment:

Codes written within multi-line comments usually begins with /* and ends with */. Within this, we can add multiple lines of statements or documentation can be added within the comment section. This /*….*/ again prevents the interpreting whatever is written within it. Mostly, the multi-line comment is used in creating documentation.

Syntax:

/* This is a comment */

Or,

/* This is a multi-line
Comment. It wont be displayed in the output. */

Example:

/* multiple lines can be added in multi line comments.
One can even comment to run alternate code*/
console.log("Learning About Comments");

Usage of comments in coding:

  • It can be used to run alternate code or block any code that is of no use.
  • It is used to provide additional information associated to the coding.
  • Multiple tricks or debugging solutions can be easily explained in comments while keeping the actual code intact.
  • If a particular section of code is not necessary or got updated, but you still want to keep it for future reference without executing, comment is the ultimate solution.
  • It can also be used for long-description and documenting new models or codes.
  • Comments help fellow developers come under one page.

Conclusion:

Practicing commenting in codes can really benefit, especially when we are in the enterprise development sectors. Developing application in the companies require team effort. There, you cannot and will not work alone. Thus, commenting specific sections of the code can help other team members understand and collaborate with efficiency and flexibility.