Real-world JSDoc documentation examples
Complete function documentation/**
* 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;
}Complete class documentation/**
* 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;
}
}Complete module documentation/**
* 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
};