Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a description to a function in javascript?

I would like to add a description to a js function like I do it in C, C++, Python ...

I do this by adding a comment at the top of the definition of the function, in the mentioned languages, but if I use JavaScript (pure, NodeJS, ReactJS) it does not show it. Ej:

Adding a Description in C++

Result (When I put the cursor in a call of the function or definition): Description being shown

But I this behaviour does not replicate with Js/NodeJs/React. Just in case I'm using Visual Studio Code.

like image 407
Antonio Roldan Avatar asked Nov 22 '25 15:11

Antonio Roldan


1 Answers

VSCode, like most IDEs, will automatically process JSDoc comments. For example:

/**
 * Does something nifty.
 *
 * @param   whatsit  The whatsit to use (or whatever).
 * @returns A useful value.
 */
function nifty(whatsit) {
    return /*...*/;
}

Here's a screenshot of what using that function is like in VSCode:

VSCode IDE showing the nifty function above being used, and showing a documentation box with the doc comments in it

You can augment that with types if you want type hints:

/**
 * Does something nifty.
 *
 * @param   {number} whatsit  The whatsit to use (or whatever).
 * @returns {string} A useful value.
 */
function nifty(whatsit) {
    return /*...*/;
}

If you use TypeScript, the types would be part of the code rather than in the JSDoc:

// TypeScript example
/**
 * Does something nifty.
 *
 * @param   whatsit  The whatsit to use (or whatever).
 * @returns A useful value.
 */
function nifty(whatsit: number): string {
    return /*...*/;
}
like image 197
T.J. Crowder Avatar answered Nov 24 '25 06:11

T.J. Crowder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!