Skip to main content

Quickstart

1. Create a JavaScript file

Save the following as book.js:

/**
* Represents a book in the library catalogue.
*/
class Book {
/**
* Create a book.
* @param {string} title - The title of the book.
* @param {string} author - The author's full name.
* @param {number} [year] - The publication year. Defaults to the current year.
*/
constructor(title, author, year = new Date().getFullYear()) {
this.title = title;
this.author = author;
this.year = year;
}

/**
* Return a human-readable summary of the book.
* @returns {string} A string in the form "Title by Author (Year)".
*/
toString() {
return `${this.title} by ${this.author} (${this.year})`;
}
}

2. Run JSDoc

Generate docsnpx jsdoc book.js

JSDoc creates an out/ directory containing a complete HTML documentation website. Open out/index.html in your browser to see the result.

3. Point JSDoc at a directory

Real projects have many source files. Use the -r (recursive) flag and specify a source directory:

npx jsdoc -r src -d docs
FlagMeaning
-rRecurse into subdirectories
srcSource directory to document
-d docsOutput directory (default: out/)

4. Exclude files

Pass a glob to --exclude to skip files or directories:

npx jsdoc -r src -d docs --exclude "**/*.test.js"

For anything beyond the simplest project, create a jsdoc.json config file and pass it with -c:

npx jsdoc -c jsdoc.json

See Configuration for the full config reference.

Summary

  1. Write `/** */` comments above your functions and classes
  2. Run `npx jsdoc -r src -d docs`
  3. Open `docs/index.html` in your browser

The Tags reference lists every @tag you can use in your comments.

Official reference: Official JSDoc documentation