Do you comment your JavaScript code?

Last updated by TiagoAraujo almost 12 years ago.See history

Comments are used to add extra information pertaining to your code. They not only make you understand your code when you look at it after a period of time, but it also help other people who might be working with you on the same project.

The goal is to write clear, concise and meaningful comments. They should describe your code in a way you or others understand what that particular piece of code does.

In other words, let your code speak for itself.

How to comment in JavaScript?

  • Single-line comments - begin with a double forward slash (//)
  • Multiline comments - start with slash-asterisk (/*) and end with asterisk-slash (*/) pair.

Generally use line comments. Use multiline comments for formal documentation and for commenting out.

Comments are ignored (not processed as coding) by the browser, however it makes you code heavier, so always try to keep your comments short by writing only what's needed to convey the idea.

document.write ("Example!"); // prints a message

Figure: Although this example sounds unnecessary for a developer, it shows clearly how comment should be kept simple and informative

We open source. Powered by GitHub