In JSDoc we need to include documentation comments in the code through which JSDoc will generate an HTML documentation website. Let’s see how to create documentation comments for different types of code.
String, Numbers & Arrays:
Javascript
const siteName = "GeeksForGeeks" ;
const number = 1000;
const myArray = [10, 20, 30];
|
Here we have generated simple JSDoc comments with the description of variables and their data type which is denoted with the help of the @type tag.
Objects:
Javascript
const site = {
id: 1,
name: "gfg" ,
rank: 1,
};
|
In the above comment, we have specified the type of all properties of an object.
Function/Methods:
Javascript
const calcAge = (current, yearOfBirth) => {
return `${current - yearOfBirth}`;
};
|
Here, the @param tag is used for documenting the different parameters of the function, whereas the @returns tag documents the return value of the function
Class:
Javascript
class Owner {
constructor(ownerDetail) {
this .name = ownerDetail.name;
this .age = ownerDetail.age;
}
printOwner() {
console.log(`Owner's name is ${ this .name}
and his age is ${ this .age}`);
}
}
|
Explanation:
- Before the declaration of the class, we have JSDoc comment to describe the class
- For Constructor, JSDoc comment similar to Function is used
- Inside the Constructor, to document variables, the @property tag is used
Linking instance of the class to its class
As we have created a class named “Owner”, so let’s create an instance of that class and link it to the class with the help of the @link tag
Javascript
const gfg = new Owner({
name: "GeeksForGeeks" ,
age: 13,
});
|
Final index.js file:
Javascript
const siteName = "GeeksForGeeks" ;
const number = 1000;
const myArray = [10, 20, 30];
const site = {
id: 1,
name: "gfg" ,
rank: 1,
};
const calcAge = (current, yearOfBirth) => {
return `${current - yearOfBirth}`;
};
class Owner {
constructor(ownerDetail) {
this .name = ownerDetail.name;
this .age = ownerDetail.age;
}
printOwner() {
console.log(`Owner's name is ${ this .name} and his age is ${ this .age}`);
}
}
const gfg = new Owner({
name: "GeeksForGeeks" ,
age: 13,
});
|
Output: Run the following jsdoc command:
npm run jsdoc
After executing the command, In the jsdoc folder, an index.html page would be created, open it in the browser to view the documentation site generated by jsdoc.
Documentation site Output:

Home page of Documentation site generated by JSDoc

Owner Class Documentation
Some common tags used in JSDoc comments:
- @author – To document the author of the code.
- @constant – To document constants.
- @default – Allows documenting the default value given to something.
- @function – This tag is used to describe the function or method.
- @global – Documents the global object.
- @implements – To document the implementation of an interface.
- @member – Documents the members of the function.
- @module – Used for documenting JavaScript module.
- @param – This tag is used to document parameters/arguments of the function.
- @returns – It is used to document the return value of the function.
- @type – To document the type of variables
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
31 Oct, 2021
Like Article
Save Article