May 20

JavaScript comments

Date: May 20, 2009. Comments»

JavaScript offers two form of comments:

  1. Block comments /* */
  2. Line ending comments //

Comments are pretty important for readability but obsolete comments are worse than no comments. The /* */ form of block comments came from a language called PL/I. PL/I chose those strange pairs as the symbols for comments because they were unlikely to occur in that language's programs, except perhaps in string literals. In JavaScript, those pairs can also occur in regular expression literals, so block comments are not safe for commenting out blocks of code.

/* var bar = 'baz'; var foo = 'foobar'.match(/\w*/g); alert(foo); */

In the above case the block comment close on the match which was not the intention. So better way is to do line ending comment(//).

Comments

Leave Comment: