Complete Examples

Real-world JSDoc documentation examples

Function Example
Complete function documentation with all common tags

Syntax:

Complete function documentation

Example:

/**
 * Calculates the area of a rectangle
 * @function calculateArea
 * @param {number} width - The width of the rectangle
 * @param {number} height - The height of the rectangle
 * @returns {number} The area of the rectangle
 * @throws {TypeError} When width or height is not a number
 * @example
 * // Calculate area of 5x3 rectangle
 * const area = calculateArea(5, 3);
 * console.log(area); // 15
 * @since 1.0.0
 * @author Jane Smith <jane@example.com>
 */
function calculateArea(width, height) {
  if (typeof width !== 'number' || typeof height !== 'number') {
    throw new TypeError('Width and height must be numbers');
  }
  return width * height;
}
Class Example
Complete class documentation with constructor and methods

Syntax:

Complete class documentation

Example:

/**
 * Represents a user in the system
 * @class User
 * @param {string} name - The user's name
 * @param {string} email - The user's email address
 * @example
 * const user = new User('John Doe', 'john@example.com');
 * console.log(user.getName()); // 'John Doe'
 */
class User {
  /**
   * Create a user
   * @constructor
   * @param {string} name - The user's name
   * @param {string} email - The user's email address
   */
  constructor(name, email) {
    this.name = name;
    this.email = email;
  }

  /**
   * Get the user's name
   * @method getName
   * @returns {string} The user's name
   */
  getName() {
    return this.name;
  }
}
Module Example
Complete module documentation with dependencies and exports

Syntax:

Complete module documentation

Example:

/**
 * User management module
 * @module UserManagement
 * @description Handles user creation and deletion
 * @requires Database
 * @requires EmailService
 * @exports createUser
 * @exports deleteUser
 * @example
 * // Create a user
 * const user = createUser('John Doe', 'john@example.com');
 * console.log(user);
 * 
 * // Delete a user
 * deleteUser(user.id);
 */
const Database = require('./Database');
const EmailService = require('./EmailService');

function createUser(name, email) {
  // Implementation here
}

function deleteUser(userId) {
  // Implementation here
}

module.exports = {
  createUser,
  deleteUser
};