How to document a javascript function with JSDoc?

 * Here comes the function description
 * @param {string} name - This name is a string
 * @returns {string} It returns a string
 * @example
 *   getName('Son-Goku')

function getName(name) {
  return name

November 10th, 2019

# Function documentation

Add to bookmarks

A good code documentation is mandatory for successful development. One way is to use JSDoc. It is a markup language used to annotate Javascript code. This can give you a brief overview of the codes functionality.

To describe a functions parameters, we use types. The basic syntax of a type is @type {typeName}, for instance @param {string}. The following table contains possible ways to write the type:

Syntax Description
@param {number} [num] Optional parameter
@param {number} [num=1] Optional parameter with default value
@param {...number} num Variable number of that type
{?number} Nullable type
{!number} Non-Nullable type
@param {(number|string)} Multiple types
@param {*} n Any type
@param {...number} num Variable number of that type
@param {string[]} n Array of strings

JSDoc provides the following default typeNames:

  • null
  • undefined
  • boolean
  • number
  • string
  • Array or []
  • Object or {}