Guide: Documenting a Legacy JavaScript Repo with JSDoc
JavaScript teams face a common problem: code grows faster than documentation. Over time, repos accumulate undocumented functions, unclear parameter types, and missing return descriptions. Adding JSDoc comments improves IDE autocomplete, TypeScript inference, and team onboarding.
Why JSDoc for JavaScript?
JSDoc is the standard documentation format for JavaScript. Modern editors like VS Code read JSDoc comments to provide:
- IntelliSense: Parameter hints and auto-completion
- Type inference: Even without TypeScript, JSDoc tags help editors understand types
- Generated docs: Tools like TypeDoc can produce HTML documentation
- IDE navigation: Jump to definitions and find usages more easily
The Documentation Challenge
Legacy JavaScript repos typically struggle with:
- No comments: Functions without any documentation
- Inline comments: Mixed-quality notes scattered in code
- Missing types: Parameters documented in words but not in @param tags
- Scale: Hundreds of functions across multiple files
A Systematic Documentation Approach
Phase 1: Discovery
Before writing docs, map what you have. Tools can identify:
- Exported functions without JSDoc
- Callbacks and event handlers missing @callback tags
- Classes missing @param and @returns tags
Phase 2: Auto-Generation
AI-powered tools can parse JavaScript AST and generate initial JSDoc blocks. These tools extract function names, parameter order, and inferred types from usage patterns.
Phase 3: Manual Review
Generated documentation is a starting point. Human review ensures:
- Complex logic is explained in plain language
- Edge cases and error conditions are documented
- Parameter descriptions match actual behavior
Phase 4: Integration
Once documentation is complete, integrate it into your workflow. Add JSDoc verification to CI checks so new code follows the same standards.
Example Documentation
Before (no documentation):
function fetchUser(id, options) {
const res = await api('/users/' + id, options);
return res.json();
}
After (with JSDoc):
/**
* Fetches a user record from the API.
*
* @param {string} id - The unique user identifier.
* @param {Object} [options] - Optional request configuration.
* @param {number} [options.timeout=5000] - Request timeout in milliseconds.
* @param {string} [options.authToken] - Bearer token for authentication.
* @returns {Promise
Tools That Help
Several approaches exist for JavaScript documentation:
- Manual writing: Precise but slow for large codebases
- Static analysis tools: Find undocumented functions but don't write docs
- AI-powered generators: Produce JSDoc drafts for human review
The Legacy Doc-AI CLI supports batch JSDoc generation with diff review, letting you document entire repositories while maintaining quality control.
Conclusion
Adding JSDoc to legacy JavaScript is a high-value investment. The process—discover, auto-generate, review, integrate—balances automation with human oversight. Consistent documentation improves developer experience and reduces onboarding time across your entire codebase.