Home
Do you comment your JavaScript code?
  v4.0 Posted at 24/08/2015 11:08 AM by William Yin

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

Related rules

    Do you feel this rule needs an update?

    If you want to be notified when this rule is updated, please enter your email address:

    Comments: