JSDoc Documentation
Jsdoc
π¨βπΌ Code without documentation is like a recipe without instructionsβtechnically
all the ingredients are there, but good luck figuring out how to use them!
JSDoc is a documentation format that lives in special comments above your
functions. The best part? TypeScript and your IDE understand JSDoc, so your
documentation shows up in tooltips and autocomplete.
/**
* Calculates the area of a rectangle.
* @param width - The width of the rectangle
* @param height - The height of the rectangle
* @returns The area in square units
* @example
* const area = calculateArea(5, 10)
* console.log(area) // 50
*/
function calculateArea(width: number, height: number): number {
return width * height
}
When you hover over
calculateArea anywhere in your codebase, you'll see this
documentation!Since TypeScript already provides type information (parameter types, return
types), JSDoc is primarily useful for descriptions and examples in
TypeScript projects. The
@param and @returns tags add human-readable
explanations that go beyond what types alone can convey.Common JSDoc Tags
| Tag | Purpose |
|---|---|
@param | Documents a function parameter |
@returns | Documents what the function returns |
@example | Shows how to use the function |
@throws | Documents errors the function throws |
@see | Links to related documentation |
π¨ Open
and:
- Add JSDoc above
addwith a description,@paramforaandb, and@returns - Add JSDoc above
greetwith a description,@paramforname,@returns, and an@example - Add JSDoc above
calculateCompoundInterestdocumentingprincipal,rate(decimal),years,@returns, and an@example - Create and export
clamp(value: number, min: number, max: number): numberthat returns:minwhenvalueis belowminmaxwhenvalueis abovemaxvaluewhen it is already betweenminandmax(inclusive)
- Give
clampcomplete JSDoc (description,@param,@returns,@example)
Required exports
add, greet, calculateCompoundInterest, clampCompletion criteria
Automated tests verify behavior (not the JSDoc text itself):
add(2, 3)β5;add(-1, 1)β0greet('Alice')β"Hello, Alice!"calculateCompoundInterest(1000, 0.05, 10)β1628.89calculateCompoundInterest(100, 0.1, 1)β110clamp(15, 0, 10)β10;clamp(-5, 0, 10)β0;clamp(5, 0, 10)β5;clamp(0, 0, 10)β0;clamp(10, 0, 10)β10
For the learning goal: hover each function in your editor and confirm the JSDoc
appears in the tooltip.
Some functions in this exercise use built-in Math helpers for powers and for
choosing the larger/smaller of two numbers when clamping a value into a range.
π° Put a brief description immediately after the opening
/**.π JSDoc Reference