Open In App

Documentation Comments in JSDoc

In JSDoc we need to include documentation comments in the code through which JSDoc will generate an HTML documentation website. Let’s see how to create documentation comments for different types of code.

String, Numbers & Arrays:






/**
 * Site Name
 * @type {string}
 */
const siteName = "GeeksForGeeks";
  
/**
 * Number
 * @type {number}
 */
const number = 1000;
  
/**
 * Array
 * @type {Array<number>}
 */
const myArray = [10, 20, 30];

Here we have generated simple JSDoc comments with the description of variables and their data type which is denoted with the help of the @type tag.

Objects:






/**
 * Site object
 * @type {{id: number, name: string, rank: number|string}}
 */
const site = {
  id: 1,
  name: "gfg",
  rank: 1,
};

In the above comment, we have specified the type of all properties of an object.

Function/Methods:




/**
 * Calculate age
 * @param {number} current Current year
 * @param {number} yearOfBirth Year of Birth
 * @returns {string} your calculated age
 */
const calcAge = (current, yearOfBirth) => {
  return `${current - yearOfBirth}`;
};

Here, the @param tag is used for documenting the different parameters of the function, whereas the @returns tag documents the return value of the function

Class:




/**
 * Class to create new owner
 */
class Owner {
  /**
   * Owner details
   * @param {Object} ownerDetail
   */
  constructor(ownerDetail) {
    /**
     * @property {string} name Owner name
     */
    this.name = ownerDetail.name;
  
    /**
     * @property {number} age Owner's age
     */
    this.age = ownerDetail.age;
  }
  
  /**
   * @property {Function} printOwner Prints Owner's details
   * @returns {void}
   */
  printOwner() {
    console.log(`Owner's name is ${this.name} 
            and his age is ${this.age}`);
  }
}

Explanation: 

Linking instance of the class to its class

As we have created a class named “Owner”, so let’s create an instance of that class and link it to the class with the help of the @link tag




/**
 * Link to Owner Class
 * See {@link Owner}
 */
const gfg = new Owner({
  name: "GeeksForGeeks",
  age: 13,
});

Final index.js file: 




// @ts-check
  
/**
 * Site Name
 * @type {string}
 */
const siteName = "GeeksForGeeks";
  
/**
 * Number
 * @type {number}
 */
const number = 1000;
  
/**
 * Array
 * @type {Array<number>}
 */
const myArray = [10, 20, 30];
  
/**
 * Site object
 * @type {{id: number, name: string, rank: number|string}}
 */
const site = {
  id: 1,
  name: "gfg",
  rank: 1,
};
  
/**
 * Calculate age
 * @param {number} current Current year
 * @param {number} yearOfBirth Year of Birth
 * @returns {string} your calculated age
 */
const calcAge = (current, yearOfBirth) => {
  return `${current - yearOfBirth}`;
};
  
/**
 * Class to create new owner
 */
class Owner {
  /**
   * Owner details
   * @param {Object} ownerDetail
   */
  constructor(ownerDetail) {
    /**
     * @property {string} name Owner name
     */
    this.name = ownerDetail.name;
  
    /**
     * @property {number} age Owner's age
     */
    this.age = ownerDetail.age;
  }
  
  /**
   * @property {Function} printOwner Prints Owner's details
   * @returns {void}
   */
  printOwner() {
    console.log(`Owner's name is ${this.name} and his age is ${this.age}`);
  }
}
  
/**
 * Link to Owner Class
 * See {@link Owner}
 */
const gfg = new Owner({
  name: "GeeksForGeeks",
  age: 13,
});

Output: Run the following jsdoc command:

npm run jsdoc

After executing the command, In the jsdoc folder, an index.html page would be created, open it in the browser to view the documentation site generated by jsdoc.

Documentation site Output:

Home page of Documentation site generated by JSDoc

Owner Class Documentation

Some common tags used in JSDoc comments:


Article Tags :