Reference Card

JSDoc

/** document everything */
structured JS comments
basics

Documenting a Function

/**
 * Calculates the total price with tax.
 *
 @param  {number} price   - Base price
 @param  {number} taxRate - Tax as decimal
 @returns {number} Total including tax
 */
function calcTotal(price, taxRate) {
  return price * (1 + taxRate);
}
types

Type Syntax

@param {string}           // primitive
@param {number}
@param {boolean}
@param {Object}           // object
@param {string[]}         // array of strings
@param {Array.<number>}   // array (alt)
@param {string|number}    // union
@param {*}                // any type
@param {?string}          // nullable
@param {!string}          // non-nullable
@returns {Promise<User>}  // promise
parameters

Optional & Default Params

/**
 @param {string}  name          required
 @param {number}  [age]         optional
 @param {boolean} [active=true] with default
 */
function createUser(name, age, active = true) {}

// Destructured object param:
/**
 @param {Object}  opts
 @param {string}  opts.name
 @param {number}  opts.age
 */
function createUser({ name, age }) {}
variables

Annotating Variables

/** @type {string} */
let username = 'alice';

/** @type {number[]} */
const scores = [];

/** @type {HTMLCanvasElement} */
const canvas = document.querySelector('canvas');

/** @type {Map<string, number>} */
const lookup = new Map();
custom types

@typedef — Define a Type

/**
 @typedef  {Object} User
 @property {number}  id
 @property {string}  name
 @property {string}  [email]  optional
 */

// Now use it anywhere:
/**
 @param   {User} user
 @returns {string}
 */
function greet(user) {
  return `Hello, ${user.name}`;
}
callbacks

@callback — Function Types

/**
 @callback Predicate
 @param    {number} value
 @returns  {boolean}
 */

/**
 @param {number[]}  arr
 @param {Predicate} fn
 */
function filter(arr, fn) {
  return arr.filter(fn);
}
classes

Documenting Classes

/**
  * Represents a 2D point.
 @class
 */
class Point {
  /**
   @param {number} x
   @param {number} y
   */
  constructor(x, y) {
    /** @type {number} */
    this.x = x;
    /** @type {number} */
    this.y = y;
  }
}
async

Async Functions

/**
  * Fetches a user from the API.
 @async
 @param   {number}       id
 @returns {Promise<User>}
 @throws  {Error}        If not found
 */
async function fetchUser(id) {
  const res = await fetch(`/users/${id}`);
  if (!res.ok) throw new Error('Not found');
  return res.json();
}
enums

Enum-like Constants

/**
  * Direction options.
 @enum {string}
 */
const Direction = {
  UP:    'up',
  DOWN:  'down',
  LEFT:  'left',
  RIGHT: 'right',
};

/**
 @param {Direction} dir
 */
function move(dir) { /* ... */ }
modifiers

Access & State Tags

/** @private   */ // not part of public API
/** @protected */ // accessible to subclasses
/** @public    */ // explicitly public
/** @readonly  */ // should not be reassigned
/** @static    */ // static member
/** @abstract  */ // must be implemented
/** @override  */ // overrides parent method
/**
 @deprecated Use newFn() instead.
 */
generics

Generic / Template Types

/**
 @template T
 @param   {T[]}      arr
 @param   {function(T): boolean} fn
 @returns {T[]}
 */
function filter(arr, fn) {
  return arr.filter(fn);
}

// Multiple type params:
/** @template K, V */
modules

Module & File Tags

/**
  * Utilities for date formatting.
 @module dateUtils
 */

/**
 @file    Entry point for the app.
 @author  Alice <alice@example.com>
 @version 1.0.0
 @license MIT
 @since   2024-01-01
 */
references

Links, Examples & See Also

/**
  * Parses a date string.
  * See {@link https://mdn.io/Date}
  * See also {@link formatDate}
 @see formatDate
 @example
 // Basic usage:
 parseDate('2024-01-01'); // → Date
 @example <caption>With timezone</caption>
 parseDate('2024-01-01', 'UTC');
 */
ts-check

TypeScript in Plain JS

// @ts-check  ← add at top of file
// enables TS type checking via JSDoc

/** @type {import('./types').Config} */
const config = { port: 3000 };

// Import types from .d.ts or TS files:
/** @typedef {import('express').Request} Req */

// Cast a type:
const el = /** @type {HTMLInputElement} */
  (document.getElementById('input'));
quick reference

All Common Tags

@paramDocument a function parameter
@returnsDocument the return value
@typeAnnotate a variable's type
@typedefDefine a reusable custom type
@callbackDefine a function signature type
@templateGeneric / type parameter
@enumDocument an enum-like object
@property / @propDocument an object property (inside @typedef)
@throws / @exceptionDocument errors the function may throw
@asyncMark a function as asynchronous
@deprecatedMark as deprecated with optional message
@private / @protected / @publicAccess level modifiers
@readonlyMark a value as read-only
@staticMark a class member as static
@abstractMark a method as abstract
@overrideMark a method as overriding parent
@class / @constructorMark a function as a constructor
@extends / @augmentsDocument class inheritance
@implementsDocument interface implementation
@exampleAdd a usage example
@seeLink to related item or URL
{@link}Inline hyperlink in description text
@moduleMark a file as a module
@file / @fileoverviewDescribe the file
@authorDocument the author
@versionDocument the version
@sinceWhen the item was added
@licenseLicense type
@todoNote something to fix or add
@ignoreExclude from generated docs