Comments

Comments are notes in your JavaScript code that are ignored by the browser. They help you explain, organize, or temporarily disable code without affecting program execution.

Types of Comments in JavaScript
Comment TypeSyntaxUsage
Single-line Comment// your comment hereComments a single line or part of a line
Multi-line Comment/* your comment here */Comments multiple lines or block of text

Single-line comments start with // and continue to the end of the line. Use them for brief explanations or notes.

📌 Deep Dive: Single-line Comment Example

JAVASCRIPT
// This variable stores the user's age
let age = 30; // age in years

Multi-line comments start with /* and end with */. Use these to comment out blocks of code or write longer explanations.

📌 Deep Dive: Multi-line Comment Example

JAVASCRIPT
/*
  This function calculates the square of a number.
  It takes one parameter:
  - num: a number to square
*/
function square(num) {
  return num * num;
}
Illustration of comments
Illustration of comments

💡 Best Practice

Use comments to clarify complex logic or important details. Avoid obvious comments that repeat the code.

⚠️ Important

Never leave sensitive information (like passwords) in comments, as they are visible in the source code.

Comments are also invaluable for temporarily disabling code during debugging:

📌 Deep Dive: Commenting Out Code

JAVASCRIPT
// console.log('This line is disabled and won\'t run');

/*
console.log('This block of code');
console.log('is temporarily disabled');
*/

💡 Summary

  • // for single-line comments
  • /* ... */ for multi-line comments
  • Comments improve code readability and maintainability
  • Use comments wisely to explain "why", not "what"