jsdoc Javascript documentation tutorials with examples

In my previous post, we learned javascript documentation framework - JSDOC tutorials, In this post, We are going to learn JSdoc function tags with examples.

function documentation

Any function can be declared with function name return types and accepted input types. @param tag provides parameters for a javascript function. This includes parameter type in open and close braces and Description of a parameter

Types of the accepted parameter are string Object or namespace pointed to code.

@param tag

The below examples specify how How to document javascript function names, types and descriptions of a function using the @param tag Below is a Function name documentation example

/**
 * @param This is a Simple function in javascript
 */
function method1() {
  console.log("Hi Simple Function");
}

Below is a Function name and type documentation example

/**
 * @param {string } This is Simple function in javascript
 */
function method1(msg) {
  console.log("Hi Simple Function" + msg);
}

Below is a Function name, type, and description example

/**
 * @param {string } method1- simple function in javascript
 */
function method1(msg) {
  console.log("Hi Simple Function" + msg);
}

Optional parameters and the Default value

The function can be applied with optional parameters and default values. documentation of a function can also include optional and default values.

/**
 * @param {string} [name= Kiran] - Function with optional Default parameters.
 */
function functionOptionalDefault(name) {
  if (!name) {
    name = "Kiran";
  }
  console.log("Hi " + name);
}

function parameter union example union type is complex type on which multiple types can be applied using symbol |

/**
 * @param {(string|string[])} [name=Kiran] - String or multiple strings
 */
function mymethod(name) {}

callback functions example

In javascript, the Function parameter can accept callback functions. The @callback tag use to specify callback type

/**
 * mycallback is a callback function defined
 *
 * @callback mycallback
 * @param {string} parameters string
 */

/**
 * Callback handles certain operations
 * @param {mycallback} mycallback - Function with callback example
 */
function functioncallbackexample(mycallback) {
  // code
}