JSDoc is a markup language used to annotate JavaScript source code files. It provides a way to document the code and its API. This cheatsheet covers the main JSDoc tags and their usage.
A basic JSDoc comment looks like this:
/**
* This is a JSDoc comment
*/
/**
* Calculates the sum of two numbers
* @param {number} a - The first number
* @param {number} b - The second number
* @returns {number} The sum of a and b
*/
function sum(a, b) {
return a + b;
}
/**
* Represents a book
* @class
*/
class Book {
/**
* Create a book
* @param {string} title - The title of the book
* @param {string} author - The author of the book
*/
constructor(title, author) {
this.title = title;
this.author = author;
}
/**
* Get the book's title
* @returns {string} The title of the book
*/
getTitle() {
return this.title;
}
}
Used to describe a function parameter.
/**
* @param {string} name - The name of the person
* @param {number} [age] - The age of the person (optional)
* @param {boolean} [isStudent=false] - Whether the person is a student (default: false)
*/
function describePerson(name, age, isStudent) {
// Function body
}
Describes the return value of a function.
/**
* @returns {Promise<string>} A promise that resolves with a greeting
*/
async function getGreeting() {
return "Hello, World!";
}
Documents exceptions that might be thrown.
/**
* @throws {Error} If the input is not a number
*/
function squareRoot(x) {
if (typeof x !== 'number') {
throw new Error('Input must be a number');
}
return Math.sqrt(x);
}
Specifies the type of a variable or property.
/** @type {string} */
let name = "John Doe";
/** @type {number[]} */
let scores = [98, 85, 91];
Defines a custom type.
/**
* @typedef {Object} Person
* @property {string} name - The person's name
* @property {number} age - The person's age
*/
/** @type {Person} */
let person = { name: "John", age: 30 };
Documents a callback function.
/**
* @callback CompareCallback
* @param {*} a - The first element to compare
* @param {*} b - The second element to compare
* @returns {number} A negative number if a < b, positive if a > b, zero if equal
*/
/**
* Sorts an array using a compare function
* @param {Array} arr - The array to sort
* @param {CompareCallback} compareFunc - The compare function
* @returns {Array} The sorted array
*/
function customSort(arr, compareFunc) {
return arr.sort(compareFunc);
}
Indicates that a function is asynchronous.
/**
* @async
* @returns {Promise<string>} A promise that resolves with data
*/
async function fetchData() {
// Function body
}
Marks a function or method as deprecated.
/**
* @deprecated since version 2.0.0, use newFunction() instead
*/
function oldFunction() {
// Function body
}
Specifies the visibility of a member.
class MyClass {
/**
* @private
*/
#privateMethod() {
// Method body
}
/**
* @protected
*/
_protectedMethod() {
// Method body
}
/**
* @public
*/
publicMethod() {
// Method body
}
}
Provides an example of how to use a function or class.
/**
* Adds two numbers
* @param {number} a - The first number
* @param {number} b - The second number
* @returns {number} The sum of a and b
* @example
* // returns 5
* add(2, 3)
*/
function add(a, b) {
return a + b;
}
Refers to another symbol or resource.
/**
* @see {@link otherFunction} for related functionality
*/
function someFunction() {
// Function body
}
Indicates tasks that need to be completed.
/**
* @todo Implement error handling
* @todo Add support for negative numbers
*/
function calculateSquareRoot(x) {
return Math.sqrt(x);
}
Indicates when a function or feature was added.
/**
* @since 1.5.0
*/
function newFeature() {
// Function body
}
Tells JSDoc to ignore the following code when generating documentation.
/**
* @ignore
*/
function internalHelper() {
// Function body
}
2024 © All rights reserved - buraxta.com