Skip to content
Advertisement

What is the correct way of code comments in JavaScript

What is the correct way of code comments in Javascript – is the same syntax as in Java? And which tools actually would take advantage of these comments:

  /*
  * Add an element to the group
  * @param {Object}  overlayElement
  * @param {Object} [element2] optional element
  */ 

I found new Resharper 6 (I write JS in VisualStudio 2010) offers the same comments as in C#, but only within the functions body, something like /// <param name="overlayElement"></param> . The JS code comments are not highlighted as such by ReSharper.

What is the best way to go …?

Advertisement

Answer

using // is better than /* */ because then you can use the latter to take out an entire block containing other comments. However, if you want to use an automatic documentation generation tool, you must use comments similar to javaDoc style.

This is an example that would work with YUI DOC (best one) https://yui.github.io/yuidoc/

/**
* This is a description
* @namespace My.Namespace
* @method myMethodName
* @param {String} some string
* @param {Object} some object
* @return {bool} some bool
*/
Advertisement