When we write code, it is a good habit to write comments. A good developer recognizes that he must write comments while writing his code. So that after some time when he or someone else reads the comments written with that code, then he can understand the whole code.


Write comments in such a way that if one reads those comments, then it is easy to understand or know what the code is written for. If you do not have the habit of writing comments yet, then make a habit of writing comments from today.


How To Write Comments in JavaScript ?


Write Comments in JavaScript

There are two types of comments in JavaScript. Which is as follows:


Single Line Comment / Inline Comment

It is represented by double forward slashes. It is used back and forth in sentence. The code written next to it does not work.

It is also called inline comment because it can be used anywhere in the line of sentence.


You can see its usage in the example below:


<script>
// It is a single line comment(added before the line or statement)
document.write("CodexRadar"); // It is a single line comment(added after the line or statement)
</script> code-box



Multi Line Comment / Block Comment

When you have to comment the code of more than one line, then we use multi line comment. We start it with forward slash and asterisk and then stop by writing asterisk and forward slash. We can also use it for single line comment.


This can be seen in the example below:


<script>

/* It is a single line comment */

/* It is a multi line comment 1.
It is a multi line comment 2. */

document.write("CodexRadar");

</script> code-box


In this way, we can write comments in JavaScript in two ways. You should also make a habit of writing comments from today.


Summary

In this post we learn how to write comments in JavaScript. You try to make it yourself. I hope you make. If you face any problem, then comment and tell us. Keep visiting the blog to read more similar posts.

Post a Comment

Previous Post Next Post