Documenting Javascript with JSDocs (Crash Course)

The document is a crash course on using JSDocs to document JavaScript code. It covers installation via npm, basic usage of JSDocs comments, and more

Crash Course on JSDoc and Using It with Svelte 5

JSDoc is a markup language used to annotate JavaScript source code files. It allows developers to add documentation directly in their code, which can then be used by IDEs for autocompletion, type checking, and generating documentation.

Installing JSDoc

npm install -g jsdoc

Basic JSDoc Syntax

JSDoc comments start with /** and end with */. They are placed immediately before the code they document.

/**
 * Adds two numbers
 * @param {number} a - The first number
 * @param {number} b - The second number
 * @returns {number} The sum of a and b
 */
function add(a, b) {
  return a + b;
}

Common JSDoc Tags

  • @param: Describes a function parameter

  • @returns: Describes the return value of a function

  • @type: Specifies the type of a variable or property

  • @typedef: Defines a custom type

Using JSDoc with Svelte 5

In Svelte 5, you can use JSDoc to document and type-check your components, including props and state. Here's how:

Documenting Props

Documenting State

Documenting Derived Values

Documenting Effects

Showing Samples with JSDoc

To show code samples using JSDoc in Svelte 5, you can use the @example tag within your JSDoc comments. Here's how to effectively use it:

For Component Documentation

For Function Documentation

For Prop Documentation

For State Documentation

For Derived Values

By using these JSDoc comments in your Svelte 5 components, you'll get improved autocompletion, type checking, and inline documentation in your IDE. This makes your code more maintainable and easier to understand for other developers (or yourself in the future).

Remember to use markdown code blocks (svelte or js) within the @example tags to properly format your code samples. This will ensure they are displayed correctly in generated documentation and IDE tooltips.

By consistently using JSDoc comments with examples throughout your Svelte 5 project, you'll create a more robust and developer-friendly codebase.

Last updated