JavaScript Comments
2 April 2025 | Category: JavaScript
What Are JavaScript Comments?
JavaScript comments are used to explain code, make it more readable, and prevent execution of specific lines of code. Comments are ignored by the JavaScript engine.
1️⃣ Types of JavaScript Comments
JavaScript supports two types of comments:
✅ Single-line comments (//
)
✅ Multi-line comments (/* ... */
)
2️⃣ Single-Line Comments (//
)
A single-line comment starts with //
. Everything written after //
on the same line is ignored.
🔹 Example:
// This is a single-line comment
let x = 5; // This variable stores a number
console.log(x); // Output: 5
3️⃣ Multi-Line Comments (/* ... */
)
A multi-line comment starts with /*
and ends with */
. It is used for longer explanations or to temporarily disable multiple lines of code.
🔹 Example:
/*
This is a multi-line comment.
It can span multiple lines.
*/
let y = 10;
console.log(y); // Output: 10
🔹 Disabling Code Using Multi-Line Comments:
/*
let a = 5;
let b = 10;
console.log(a + b); // This code will not run
*/
console.log("Code execution continues...");
4️⃣ Why Use Comments?
✔️ Improve code readability
✔️ Explain complex logic
✔️ Temporarily disable code for debugging
✔️ Collaborate effectively in team projects
5️⃣ Best Practices for Writing Comments
✅ Use meaningful comments
❌ Avoid excessive commenting (don’t explain obvious things)
✅ Update comments when code changes
✅ Use comments to structure large code blocks
🚀 Conclusion
- Single-line comments (
//
) are for short explanations. - Multi-line comments (
/* ... */
) are for longer descriptions or disabling multiple lines of code. - Good comments help developers understand and maintain code better!