Questions tagged [jsdoc]

JSDoc is a markup language for adding inline API documentation to JavaScript source code. This is distinct from the various tools that parse and manipulate code that follows the JSDoc syntax.

The JSDoc markup language is similar to the Javadoc syntax, used for documenting Java code, but is specialized to work with JavaScript's more dynamic syntax and therefore unique, as it is not completely compatible with Javadoc. However, like Javadoc, JSDoc allows the programmer to create doclets and tags which can then be translated into published output, like HTML or RTF.

Example:

/**
    Represents a book.
    @constructor
    @param {string} title - The title of the book.
    @param {string} author - The author of the book.
 */
function Book(title, author) {
}

The following annotations are commonly used in modern JSDoc, but the full list varies between implementations:

  • @param Documents a method parameter; a datatype indicator can be added between curly braces
  • @return Documents a return value
  • @constructor Marks a function as a constructor
  • @deprecated Marks a method as deprecated
  • @private Signifies that a method is private
  • @requires Describe a required resource.
  • @this Specifies the type of the object to which the keyword "this" refers within a function.
  • @throws Documents an exception thrown by a method
  • @exception Synonym for @throws
  • @author Developer's name
  • @version Provides the version number of a library
1697 questions
472
votes
6 answers

How to describe "object" arguments in jsdoc?

// My function does X and Y. // @params {object} parameters An object containing the parameters // @params {function} callback The callback function function(parameters, callback) { } But how do I describe how the parameters object should be…
Andy Hin
  • 30,345
  • 42
  • 99
  • 142
244
votes
3 answers

JSDoc: Return object structure

How can I tell JSDoc about the structure of an object that is returned. I have found the @return {{field1: type, field2: type, ...}} description syntax and tried it: /** * Returns a coordinate from a given mouse or touch event * @param …
186
votes
1 answer

How to specify an array of objects as a parameter or return value in JSDoc?

In JSDoc, the best documentation I can find shows to use the following if you have an array of a specific type (such as an array of strings) as: /** * @param {Array.} myStrings All my awesome strings */ function blah(myStrings){ …
Ray
  • 40,256
  • 21
  • 101
  • 138
183
votes
6 answers

How to indicate param is optional using inline JSDoc?

According to the JSDoc wiki for @param you can indicate a @param is optional using /** @param {String} [name] */ function getPerson(name) { } and you can indicate a param inline using function getPerson(/**String*/ name) { } And I can…
studgeek
  • 14,272
  • 6
  • 84
  • 96
157
votes
8 answers

Is there a way to generate JSDoc comments in Visual Studio Code

I am currently developing a NodeJS project and found out that there is no built in functionality to create JSDoc comments for functions/methods. I am aware of the TypeScript definitions that exist but I couldn't really find anything to match what I…
zerefel
  • 1,699
  • 2
  • 11
  • 13
154
votes
5 answers

How to document a string type in jsdoc with limited possible values

I am having a function that accepts one string parameter. This parameter can have only one of a few defined possible values. What is the best way to document the same? Should shapeType be defined as enum or TypeDef or something…
137
votes
3 answers

Document destructured function parameter in JSDoc

Previously I've always documented my object parameters as follows: /** * Description of the function * * @param {Object} config - The configuration * @param {String} config.foo * @param {Boolean} [config.bar] - Optional value * @return…
morkro
  • 4,336
  • 5
  • 25
  • 35
111
votes
5 answers

How to return void in JsDoc?

Is there a specified way to declare a method or a function to return void in JsDoc? Currently I am in the belief that void is the default return value, and other return values must be specifically provided: /** * @return {Integer} The identifier…
Tower
  • 98,741
  • 129
  • 357
  • 507
109
votes
2 answers

How to document a dictionary in JSDoc?

Having next example: var CONF = { locale: { "en": { name: "English", lang: "en-US" }, "es": { name: "Spanish", lang: "es-ES" } } }; And knowing that what the…
Áxel Costas Pena
  • 5,886
  • 6
  • 28
  • 59
103
votes
4 answers

Correct way to document open-ended argument functions in JSDoc

Let's say you have something like the following: var someFunc = function() { // do something here with arguments } How would you correctly document that this function can take any number of arguments in JSDoc? This is my best guess, but I'm not…
kflorence
  • 2,187
  • 2
  • 16
  • 15
103
votes
5 answers

What's the proper way to document callbacks with jsdoc?

I've spent quite a while scouring the internet looking for the best way to properly document callbacks with jsdoc, but unfortunately, I haven't found a great one yet. Here's my question: I'm writing a Node.js library for developers. This library…
rdegges
  • 32,786
  • 20
  • 85
  • 109
100
votes
6 answers

How to specify resolution and rejection type of the promise in JSDoc?

I have some code that returns a promise object, e.g. using Q library for NodeJS. var Q = require('q'); /** * @returns ??? */ function task(err) { return err? Q.reject(new Error('Some error')) : Q.resolve('Some result'); } How to document…
Arikon
  • 1,213
  • 2
  • 9
  • 10
93
votes
4 answers

How to "import" a typedef from one file to another in JSDoc using Node.js?

Let's say I have a file named "File1.js". In this file, I export an object of objects and I give each object a typedef, like so. /** * My typedef for each object. * @typedef {Object} MyObject1 * @property {String} username Your username *…
FireController1847
  • 1,458
  • 1
  • 11
  • 26
89
votes
7 answers

Best way to document anonymous objects and functions with jsdoc

Edit: This is technically a 2 part question. I've chosen the best answer that covers the question in general and linked to the answer that handles the specific question. What is the best way to document anonymous objects and functions with…
Josh Johnson
  • 10,729
  • 12
  • 60
  • 83
82
votes
1 answer

What is the proper/canonical formatting of long JSDoc lines?

All of the official JSDoc examples have naively simple documentation strings, like the following: /** * @param {string} author - The author of the book. */ The problem is, in real-life documentation you often have longer documentation…
machineghost
  • 33,529
  • 30
  • 159
  • 234
1
2 3
99 100