How to write comment in JavaScript?

How to write comment in JavaScript?

There are two types of comments in JavaScript that one can write.

In order to write a single line comment in JavaScript, one has to type two slashes "//" in front of the code or text. This line of text or comment will then be ignored by the JavaScript interpreter.

For example:

<script type="text/javascript">

<!--

// This is a single line JavaScript comment

In order to write a multi-line comment, one has to begin the comment with /* and end it with */. The text between /* and */ will be ignored by JavaScript. The benefit of this is that one can write a longer comment than what would fit on a single line, and not have to break it up in order to fit on a single line.

For example:

<script type="text/javascript">

<!--

/*this is a multi-line comments

which will be ignored

by JavaScript;*/

//-->

</script>

These comments are visible only in the code but not in browser. These comments can be useful to clarify or indicate something, such as changes to be made to either yourself or to somebody else. It can be used to communicate with other developers or to pass on instructions. Hence, comments can be quite useful.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.