Sub topic 5
Examples, Errors, Links, and Deprecation
Type tags like @param and @returns tell a reader what goes in and what comes out. They do not show how to call a function, what can go wrong, where to read more, or whether the function should still be used. This chapter covers the five tags that carry that information: @example, @throws, @see, {@link}, and @deprecated.
This chapter assumes you can already write a JSDoc block with a description, @param, and @returns, and that you know the basic type syntax such as {string} and {number}.
A complete example
Here is a JSON parser documented with all five tags, along with an older function that is being phased out:
/**
* Parses a JSON string.
* @param {string} text - The JSON text.
* @returns {unknown} The parsed value.
* @throws {SyntaxError} If the text is not valid JSON.
* @example
* parseJson('{"a": 1}'); // => { a: 1 }
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse JSON.parse}
*/
function parseJson(text) {
return JSON.parse(text);
}
/**
* @deprecated Use {@link parseJson} instead.
* @param {string} text
*/
function oldParse(text) {
return parseJson(text);
}
console.log(parseJson('{"a": 1}')); // { a: 1 }The sections below go through each tag.
@example: show a real call
@example holds everything up to the next tag, and generators treat it as code. Whitespace and line breaks are kept, so you can write several lines:
/**
* Clamps a number into a range.
* @param {number} value
* @param {number} min
* @param {number} max
* @returns {number}
* @example
* clamp(5, 0, 10); // => 5
* clamp(-3, 0, 10); // => 0
* @example
* // Works with decimals too
* clamp(1.5, 0, 1); // => 1
*/
function clamp(value, min, max) {
return Math.min(Math.max(value, min), max);
}You can repeat the tag. Each @example shows up as its own block in generated docs and in editor hovers. A common convention is to show the result in a // => comment.
@throws: document the failure path
@throws {Type} description names an error the function can raise and the condition that causes it. Include errors that the caller is expected to handle, and include errors that come from code you call, such as the SyntaxError from JSON.parse. You can list several @throws tags.
A common mistake is to assume TypeScript enforces this tag. It doesn't. @throws is documentation only, and no checker confirms that the function really throws that type.
@see and {@link}: cross-references
@see is a block tag that adds a "See also" entry. {@link} is an inline tag, so it can appear inside any description. Both accept either a symbol name or a URL:
| Form | Meaning |
|---|---|
| `{@link parseJson}` | Link to a symbol in your code |
| `{@link parseJson the parser}` | Symbol link with custom text |
| `{@link https://example.com Docs}` | URL with link text |
| `@see parseJson` | "See also" entry pointing at a symbol |
VS Code renders symbol links as clickable references in hovers. The jsdoc HTML generator turns them into anchors. If you misspell a symbol name, the link quietly falls back to plain text, so check your links after you rename something.
@deprecated: retire an API gently
@deprecated marks a symbol that callers should stop using. Always say what to use instead, ideally with {@link}. TypeScript-aware editors draw deprecated names with a strikethrough at every call site, which helps people migrate without breaking anyone's code.
Common errors
- Putting an example on the same line as the tag. Generators usually handle this, but starting the code on the next line keeps it formatted the same way everywhere.
- Using
@linkwithout braces. A bare@linkat the start of a line reads as a block tag and breaks the text around it. Write{@link name}. - Leaving out the replacement. A bare
@deprecatedtells people to stop using something without telling them what to use. - Letting
@throwsdrift. If you change the validation in the function, update the tag in the same commit.
Exercise
Write a JSDoc block for a function toInt(text) that returns an integer, throws a RangeError when the result is NaN, includes one usage example, and links to MDN's parseInt page. Then deprecate an older legacyToInt in favor of it.
Show answer
/
Converts text to an integer.
@param {string} text - Decimal digits.
@returns {number} The parsed integer.
@throws {RangeError} If the text does not contain a number.
@example
toInt('42'); // => 42
@see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt parseInt}
/
function toInt(text) {
const n = parseInt(text, 10);
if (Number.isNaN(n)) throw new RangeError(Not a number: ${text});
return n;
}
/
@deprecated Use {@link toInt} instead.
@param {string} text
*/
function legacyToInt(text) { return toInt(text); }
Exercise
A teammate writes "@deprecated" on a function and expects older callers to get warnings when the code runs. What actually happens, and how could they get a runtime warning?
Show answer
Nothing happens at runtime. JSDoc tags are comments. Editors that use TypeScript will show a strikethrough and a hover note, and that is all. To warn at runtime, call console.warn or wrap the function with Node's util.deprecate.