Comments provide useful context to your code so you and other developers can better understand the purpose of that code upon review.
Comments can be used to provide information about a line of code, such as the intended function and possible inputs and outputs.
Comments can also be used to hide code from being executed without having to remove it from the document.
To create a comment in an HTML document, use the <!– –> tags.
<!-- This is a comment in an HTML document. -->
To create a comment in a JavaScript document, use the // or /* */ tags.
// This is a single line comment in a JavaScript document.
/*
This is
a multiline comment
in a JavaScript document.
*/
A well-commented document is one that is easy to understand and debug.
Avoid providing a comment for every line, especially lines where the code is self-documenting (due to variables with naming conventions that describe their purpose).
Comments are best used as an introduction to functions (describing the inputs, outputs and behaviour), and before any other section of complex logic.
Comments are also good for leaving messages for yourself or other developers, such as in the case of marking TODOs (sections of code which still need to be written!).