Parameters & Returns

Document function parameters, return values, and exceptions

@param
Documents function parameters with types and descriptions

Syntax:

@param {type} name Description

Example:

/**
 * @param {number} x - The first number
 * @param {number} y - The second number
 * @param {Object} [options] - Optional configuration
 * @param {boolean} [options.strict=false] - Use strict mode
 */
function calculate(x, y, options = {}) {
  // implementation
}
@returns
Documents the return value and its type

Syntax:

@returns {type} Description

Example:

/**
 * @param {number[]} numbers - Array of numbers
 * @returns {number} The sum of all numbers
 * @returns {null} Returns null if array is empty
 */
function sum(numbers) {
  if (numbers.length === 0) return null;
  return numbers.reduce((a, b) => a + b, 0);
}
@throws
Documents exceptions that may be thrown

Syntax:

@throws {ErrorType} Description

Example:

/**
 * @param {string} email - User email address
 * @throws {TypeError} When email is not a string
 * @throws {Error} When email format is invalid
 */
function validateEmail(email) {
  if (typeof email !== 'string') {
    throw new TypeError('Email must be a string');
  }
  if (!email.includes('@')) {
    throw new Error('Invalid email format');
  }
}
@yields
Documents what a generator function yields

Syntax:

@yields {type} Description

Example:

/**
 * @generator
 * @yields {number} The next number in the Fibonacci sequence
 * @returns {Generator<number, void, unknown>}
 */
function* fibonacci() {
  let a = 0, b = 1;
  while (true) {
    yield a;
    [a, b] = [b, a + b];
  }
}