JavaScript Comments
Comments in JavaScript are used to describe your code, explain logic, or temporarily disable parts of it. They are ignored during execution and help make your code more readable and maintainable.
💬 1. Single-Line Comments
Use //
to write single-line comments.
// This is a single-line comment
let x = 10; // Declaring a variable
📝 2. Multi-Line Comments
Use /* ... */
to write multi-line comments.
/*
This is a multi-line comment.
It can span multiple lines.
*/
let y = 20;
⚙️ 3. Disabling Code with Comments
Comments are often used to "turn off" code for testing/debugging:
// console.log("This won't run");
let name = "Alice";
console.log(name); // This will run
🔍 4. Good Commenting Practices
- Use comments to explain "why", not just "what"
- Keep them concise and relevant
- Update or remove outdated comments
- Use comments to separate sections of code
💡 Tip: Clean, self-explanatory code reduces the need for excessive comments.