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 docs
npx jsdoc book.jsJSDoc 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
| Flag | Meaning |
|---|---|
-r | Recurse into subdirectories |
src | Source directory to document |
-d docs | Output 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"
5. Use a config file (recommended)
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
- Write `/** */` comments above your functions and classes
- Run `npx jsdoc -r src -d docs`
- Open `docs/index.html` in your browser
The Tags reference lists every @tag you can use in your comments.
Official reference: Official JSDoc documentation