Sub topic 2
Documenting Parameters and Return Values
This chapter assumes you can write JavaScript functions with default values and destructuring, and that you know the JSDoc block structure from the previous chapter: a /** ... */ comment placed directly above the code, with a description followed by tags. The type syntax inside the braces gets a fuller treatment in the next chapter. Here you will use simple types such as {number} and {string}.
The @param tag
Each parameter gets one @param tag. The tag has three parts in a fixed order:
- The type in braces, such as
{number}. - The parameter name, which must match the name in the function signature exactly.
- An optional description. A hyphen before it is conventional and makes it easier to read.
/**
* Calculates a price including tax.
* @param {number} price - The base price.
* @param {number} [taxRate=0.2] - Tax rate as a fraction.
* @returns {number} The price with tax applied.
*/
function withTax(price, taxRate = 0.2) {
return price * (1 + taxRate);
}
console.log(withTax(100)); // 120
console.log(withTax(100, 0.1)); // 110List the tags in the same order as the parameters. Tools match tags by name, but readers scan them in order.
Optional parameters and defaults
Square brackets around the name mark a parameter as optional: [taxRate]. Put =value inside the brackets to record the default: [taxRate=0.2]. Editors show the default in tooltips, and TypeScript treats the argument as omittable.
The default in the comment is documentation only. JavaScript uses the default in the signature. If you change taxRate = 0.2 to taxRate = 0.25 and leave the comment alone, the two disagree and nothing warns you. Linting and type checking, covered in later chapters, catch some of this drift.
Object and destructured parameters
When a function takes an options object, document the object first and then each property using dot notation:
/**
* Formats a person's name.
* @param {Object} options - Formatting options.
* @param {string} options.first - Given name.
* @param {string} options.last - Family name.
* @param {boolean} [options.upper=false] - Uppercase the result.
* @returns {string} The formatted name.
*/
function formatName({ first, last, upper = false }) {
const name = `${first} ${last}`;
return upper ? name.toUpperCase() : name;
}
console.log(formatName({ first: 'Ada', last: 'Lovelace' })); // Ada LovelaceA destructured parameter has no name in the signature, so you choose one (options here) and use it as the prefix for every property. The prefix must be the same on every line, or the properties will not attach to the parent.
Rest parameters
For a rest parameter, put ... before the type:
/**
* Adds any number of values.
* @param {...number} values - Numbers to add.
* @returns {number} The sum, or 0 when called with no arguments.
*/
function sum(...values) {
return values.reduce((total, n) => total + n, 0);
}
console.log(sum(1, 2, 3)); // 6The type describes each element, not the array. {...number} is correct and {...number[]} is wrong.
The @returns tag
@returns (alias @return) takes a type and a description. It does not take a name. Describe what the value means, especially edge cases: what comes back on empty input, or when the result can be undefined.
| Situation | What to write | Column |
|---|---|---|
| Function returns a value | `@returns {Type} Meaning of the value.` | |
| Function returns nothing | Omit the tag, or write `@returns {void}` | |
| Async function | `@returns {Promise<Type>}` with the resolved type | |
| Result can be missing | Include it in the type, e.g. `{string \ | undefined}` |
For async functions, the return type is the Promise, not the value it resolves to. If you write {User} when the function returns Promise<User>, the type checker will report it.
Common errors
- Name mismatch. Renaming a parameter without updating its tag leaves an orphaned
@param. Editors then show no documentation for the real parameter. - Naming the return.
@returns {number} total The summakes "total" part of the description. - Missing tags. Documenting two of three parameters looks complete but is not. Document every parameter or none.
- Repeating the name as the description.
@param {string} name - The nametells the reader nothing new. Describe constraints, units, or format instead.
Exercise
Write a JSDoc block for function repeat(text, times = 1, { separator = '' } = {}), which returns text repeated times times, joined by separator.
Show answer
/*
Repeats a string.
@param {string} text - The string to repeat.
@param {number} [times=1] - How many copies to produce.
@param {Object} [options] - Join options.
@param {string} [options.separator=''] - Inserted between copies.
@returns {string} The repeated string.
/
The options object is optional because it defaults to {}, so both it and its property use square brackets.
Exercise
What is wrong with @param {...string[]} names for function greetAll(...names), and with @returns {string} message on a function that returns a string?
Show answer
For rest parameters, the type names each element, so it should be {...string}. @returns takes no name, so "message" becomes part of the description. Write a real description instead, such as @returns {string} The greeting text.