Commenting JavaScript code
You have multiple options to include comments in your code:
// Single line comment /* Multiline comment */
If you are new to JavaScript, I recommend you use a lot of comments to help you understand what is going on in your code. As you become more experienced, you will need fewer comments. Comments also help other developers to read and understand your code.
Using JSDoc
If you need guidance on how to write good comments, you can use the JSDoc (https://jsdoc.app/) syntax. Another additional benefit of using JSDoc is that you can use it to autogenerate documentation for your code.
This is quite a popular solution. For example, Lodash uses this approach. Use the following links to check out how the _.chunk
method is documented:
- JSDoc in practice: https://github.com/lodash/lodash/blob/4.17.15/lodash.js#L6818
- Documentation automatically generated by JSDocs: https://lodash.com/docs/4.17.15#chunk
In the next section, we will learn...