- Single-line comments: These are comments that begin with two forward slashes (//) and continue until the end of the line. They are used for short, descriptive notes.
- Multi-line comments: These are comments that begin with a forward slash and an asterisk (/*) and end with an asterisk and a forward slash (*/). They can span multiple lines and are useful for longer explanations or for temporarily disabling blocks of code.
Examples of Single-line Comments
// This is a single-line comment var x = 5; // This is another single-line comment
Examples of Multi-line Comments
/* This is a multi-line comment that spans multiple lines */ var x = 5; /* This is another multi-line comment that spans multiple lines */ var y = 10;
- Use comments sparingly, only when they add value to the code.
- Write clear and concise comments that explain the intent of the code.
- Avoid redundant comments that simply describe what the code is doing.
- Use single-line comments for short notes and multi-line comments for longer explanations.
- Avoid commenting out blocks of code as this can lead to confusion and errors when the code is revisited.