Skip to content

Commit f35e2e5

Browse files
committed
new post: 2025-03-18-mastering-jsdoc-for-javascript-documentation.md
1 parent 25a5559 commit f35e2e5

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
---
2+
title: Mastering JSDoc for JavaScript Documentation
3+
date: 2025-03-18T11:37:55.000Z
4+
categories:
5+
- Development
6+
- JavaScript
7+
- Documentation
8+
tags:
9+
- JSDoc
10+
- JavaScript
11+
- TypeScript
12+
- Coding Standards
13+
---
14+
15+
# Introduction to JSDoc
16+
17+
When working with JavaScript and TypeScript, maintaining clear and effective documentation is essential for enhancing code readability and collaboration. One of the most commonly used standards for documentation is **JSDoc**. This set of conventions allows developers to annotate their JavaScript code in a way that makes it easily understandable by other developers, as well as tools like VS Code, TypeScript, ESLint, and various documentation generators.
18+
19+
## What is JSDoc?
20+
21+
JSDoc is a standardized way to create documentation for JavaScript code through specially formatted comments. By utilizing JSDoc, you can provide detailed descriptions of functions, classes, and variables, including their parameters, return types, and potential exceptions they may throw.
22+
23+
## Syntax for JSDoc Comments
24+
25+
To create JSDoc comments in your JavaScript or TypeScript code, use the following syntax:
26+
27+
```javascript
28+
/**
29+
* Describes what the function does.
30+
* @param {string} name - The name of the person.
31+
* @param {number} age - The age.
32+
* @returns {string} Greeting text.
33+
*/
34+
function greet(name, age) {
35+
return `Hi ${name}, you are ${age} years old.`;
36+
}
37+
```
38+
39+
With this clear structure, anyone reading your code can quickly understand the purpose of the function and how to use it.
40+
41+
## Common JSDoc Tags
42+
43+
JSDoc supports several tags that serve different purposes. Here’s a quick overview of some of the most commonly used tags:
44+
45+
| Tag | Meaning |
46+
|-------------|-----------------------------------|
47+
| `@param` | Describes a parameter of the function |
48+
| `@returns` | Describes the return value of the function |
49+
| `@typedef` | Defines a custom type |
50+
| `@type` | Specifies the type of a variable |
51+
| `@deprecated` | Marks an item as outdated |
52+
| `@example` | Provides an example usage |
53+
| `@throws` | Indicates which errors may be thrown |
54+
| `@see` | References additional documentation |
55+
56+
## Examples
57+
58+
### Documenting a Function
59+
60+
Here is an example of how to document a simple addition function:
61+
62+
```javascript
63+
/**
64+
* Adds two numbers.
65+
* @param {number} a - The first number.
66+
* @param {number} b - The second number.
67+
* @returns {number} The sum.
68+
*/
69+
function add(a, b) {
70+
return a + b;
71+
}
72+
```
73+
74+
### Documenting an Object or Variable
75+
76+
You can also document objects or variables with JSDoc annotations:
77+
78+
```javascript
79+
/** @type {{name: string, active: boolean}} */
80+
const user = {
81+
name: "Alice",
82+
active: true
83+
};
84+
```
85+
86+
### Custom Types with @typedef
87+
88+
For more complex data structures, you can define your own types using `@typedef`:
89+
90+
```javascript
91+
/**
92+
* @typedef {Object} Product
93+
* @property {string} name
94+
* @property {number} price
95+
*/
96+
97+
/** @type {Product} */
98+
const p = {
99+
name: "Coffee",
100+
price: 3.99
101+
};
102+
```
103+
104+
## Bonus: Autocompletion in VS Code
105+
106+
Using JSDoc not only improves code documentation but also enhances the development experience in VS Code. Here are some of the benefits you can expect:
107+
108+
- **Tooltips**: Get immediate descriptions of functions and types.
109+
- **Type Completion**: VS Code can suggest types as you write your code.
110+
- **Type Warnings**: The editor will flag any mismatched types when parameters are passed incorrectly.
111+
112+
## Conclusion
113+
114+
JSDoc is an invaluable tool for anyone working with JavaScript or TypeScript. By incorporating clear documentation into your code, you enhance its maintainability and usability. If you are interested in exploring how to use JSDoc with classes, modules, or how to generate documentation in different formats such as HTML, feel free to delve deeper into this powerful documentation standard!

0 commit comments

Comments
 (0)