Writing Documentation for a JavaScript Project: From A to Z

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why Documentation Is Not Just a Formality?



In the world of JavaScript and Node.js development, there is often a belief that "the code should speak for itself." In practice, this rarely works. Good documentation is not just explanations for functions, but a full-fledged tool that saves hours, days, and even weeks of work for the entire team.



Imagine returning to your project after 6 months. Without documentation, you will waste time deciphering your own code. And what about new developers joining the project? High-quality documentation reduces onboarding time by 50-70%.



In this article, we will break down the complete cycle of creating documentation for a JavaScript project: from basic code comments to automatic documentation generation using modern tools.



1. Basics: JSDoc and Code Comments



The first and most important step is documenting the code at the source level. The de facto standard in the JavaScript world is JSDoc — an annotation system that allows you to describe types, parameters, and return values of functions.



Basic JSDoc Syntax



Let's look at a simple example of a function for working with an array of numbers:



/** * Calculates the arithmetic mean of an array of numbers. * @param {number[]} numbers - Array of numbers for calculation. * @param {Object} [options] - Additional options. * @param {boolean} [options.skipZeros=false] - Ignore zero values. * @returns {number} The arithmetic mean value. * @throws {Error} If the array is empty. */function calculateAverage(numbers, options = {}) {    const { skipZeros = false } = options;        if (numbers.length === 0) {        throw new Error('Array cannot be empty');    }

const filtered = skipZeros ? numbers.filter(n => n !== 0) : numbers; const sum = filtered.reduce((acc, val) => acc + val, 0); return sum / filtered.length;}


Basic JSDoc tags you need to know:



  • @param {type} name - description — description of a function parameter
  • @returns {type} description — return value
  • @throws {type} description — possible exceptions
  • @example — usage example
  • @deprecated — mark for deprecated code
  • @type {type} — type of a variable or property


Documenting Classes and Modules



For classes and modules, JSDoc also provides powerful capabilities:



/** * @module userService * Module for working with users. */

/** * Class for managing users. * @class */class UserService { /** * Creates an instance of the service. * @param {Object} config - Connection configuration. * @param {string} config.dbUrl - Database URL. */ constructor(config) { this.dbUrl = config.dbUrl; }

/** * Finds a user by ID. * @async * @param {string} userId - Unique user identifier. * @returns {Promise<Object|null>} User object or null. */ async findUserById(userId) { // Method implementation }}


2. Creating README and Main Documentation



After the code is documented, it is necessary to create external documentation. Start with README.md — this is the first thing a developer sees when opening your project.



Structure of an Ideal README



Here is a minimum set of sections that every README should have: