Loading
Current section: 5. Functions 7 exercises
solution

Jsdoc

Transcript

00:00 Let's jump right into documenting stuff with JS doc. So, we're going to add our JS doc block. So that is a back, or forward slash star star and, then it closes with a star forward slash. And it's formatted typically like this where, the additional lines have an extra space because I don't know. It looks pretty.

00:23 I always do this. And so, first you start out, the top line is the description of what your function does. Now our function is an add function that adds two numbers together. And then you have, the different parameters and so these are in order. First parameter is a.

00:39 So you say param and then the parameter, value and then you give it a description. The first number, the second number. And then you can say what it returns, the sum of two numbers. And, as AI is showing us here, you can also give it an example which is kinda nice. And so here, what makes this so valuable is that when I'm calling it later I can say add and look at what what I've got here.

01:02 I've got, a number, b number and it returns a number. Okay. That's that's nice. The first, number is a. Adds two numbers together, returns the sum of the two numbers and here's an example.

01:14 That's quite nice. That that helps a lot in using it. If we didn't have this, then all I would get is, like anything. That that's it. That's that's all.

01:25 So by having JS doc, it just really helps in, giving something actually really useful for people. As do number together, it's got a parameter a and b, first and second number returns a sum. Here's an example. So if you're building a library that you're expecting other people to use your functions, you should absolutely be JS docking those functions. Okay.

01:46 Let's go ahead and do this one as well. Yep. Here we go. We've got our example, everything. We talked about examples already, so, that's as much as we're gonna talk about there.

01:55 And then we've got our calculate compound interest. Here's the description. Here are the parameters, and what it returns. Here's an example. Pretty much just reps at this point.

02:07 And then we're gonna make a new function called clamp. And our AI is really good at writing these kinds of functions. So, clamps value between min and max, here's a value. Here's the min and max, returns a clamp value, an example. The JS doc itself can get a little bit more complicated when we're dealing with objects and arrays and stuff like that.

02:27 So, like, some more advanced types can make this a little bit more complicated. But, it is, like, something that your AI agent should be able to, handle pretty well for you and it's pretty sensible once you get used to it. And in fact, you can actually avoid, writing dot t s files and still get the value of typed JavaScript with dot j s files by using JS doc because TypeScript actually understands JS doc and you can explicitly, give a type in the JS doc for this. So you could say the value is a string just or is a number, just by using JS doc which is pretty cool and some libraries will do that. So you'll run into some libraries that have dot JS files and they're using JS doc heavily and they are still, if they do it right, they are still technically typed and type safe with TypeScript.

03:16 I don't recommend doing that myself. Just use TS everywhere. It runs, everywhere except for the browser, but you're gonna need a build for the browser anyway. So, with that, I think I think you know everything you need to know about JSDoc. So great job with this one.