Sub topic 1
Anatomy of a JSDoc Comment
JSDoc is a convention for writing structured comments in JavaScript. Tools read these comments to build documentation sites, show hints in your editor, and check types. All of that depends on the comment having the right shape. This chapter covers that shape: how a JSDoc block starts, where it goes, and how its parts fit together.
Prerequisites
This chapter assumes you can already:
- Write JavaScript functions, objects, and ES classes.
- Use ES modules (
importandexport). - Run commands with npm or npx.
- Work in a code editor such as VS Code.
You do not need any JSDoc experience.
The Opening Delimiter Matters
A JSDoc comment is a block comment that starts with exactly /**, two asterisks. Tools treat every other comment style as an ordinary comment and skip it.
// A line comment: ignored by JSDoc tools.
/* A plain block comment: also ignored. */
/**
* A JSDoc comment: parsed by JSDoc tools.
*/
function documented() {}
/*** Three or more asterisks: ignored by the jsdoc tool. */
function notDocumented() {}The leading * on each inner line is optional. Tools strip it out. Most teams keep it because it makes the block easy to read and editors add it for you.
The Three Parts of a Block
A JSDoc block holds up to three kinds of content:
- Description. Free text at the top of the block that says what the code does. The first sentence works as a summary, and many tools show it on its own in lists and tooltips.
- Block tags. Lines that start with
@, such as@paramor@returns. Each one records one structured fact. A tag continues until the next block tag or the end of the comment. - Inline tags. Tags wrapped in braces, such as
{@link parseJson}, that sit inside a description or a tag's text. They mark up part of a sentence and do not start a new section.
Here is a complete block with all three parts:
/**
* Calculates a price including tax.
*
* Rounding is left to the caller. See {@link withTax} for the formula.
* @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)); // 120The next chapter covers what each @param and @returns field means. For now, focus on the order: description first, then block tags.
Placement: Directly Above the Code
A JSDoc block documents whatever comes right after it. That can be a function declaration, a class, a method, a variable, or an exported value. If you put other code or a different comment between the block and its target, the attachment breaks.
/**
* Returns the current year.
*/
const currentYear = () => new Date().getFullYear();
console.log(currentYear());This placement rule is also why your editor shows the description when you hover over currentYear anywhere in the file.
Common Mistakes
| Mistake | What happens | Fix |
|---|---|---|
| Starting with `/*` | Tools ignore the comment entirely | Use `/**` |
| Starting with `/***` | The jsdoc tool ignores it | Use exactly two asterisks |
| Writing description text after a tag | The text becomes part of that tag's description | Put the main description before any block tag |
| Leaving a blank line of code between the block and the target | The block attaches to nothing, or to the wrong thing | Keep the block directly above its target |
| Writing `@link` without braces in prose | It is read as a block tag and ends the description | Write `{@link name}` |
Exercises
Exercise
A teammate writes /* Adds two numbers. @param {number} a */ above a function, and nothing appears on hover. What is wrong?
Show answer
The comment starts with /*, not /**, so JSDoc tools treat it as an ordinary comment. Change the opening to /**.
Exercise
In this block, which text is the main description and which belongs to a tag? /** @param {string} id - The record ID. Fetches a record from the cache. */
Show answer
There is no main description. Because "Fetches a record from the cache." comes after @param, it is part of the id parameter's description. Move it above the tag.
Exercise
What is the difference between @see written on its own line and {@link parseJson} inside a sentence?
Show answer
@see is a block tag. It starts its own section of the comment. {@link parseJson} is an inline tag that creates a reference inside the surrounding text and does not end the description or the tag it sits in.