
Let’s go through Comments in JavaScript step by step.
📝 Comments in JavaScript
A comment is text in your code that is ignored by JavaScript during execution.
👉 They are used to explain code, make notes, or temporarily disable code.
1️⃣ Single-Line Comment (//
)
- Starts with
//
- Everything after
//
on that line is ignored.
// This is a single-line comment
let x = 10; // Declaring a variable
console.log(x); // Printing value of x
2️⃣ Multi-Line Comment (/* ... */
)
- Starts with
/*
and ends with*/
- Can span multiple lines.
/*
This is a multi-line comment.
Useful for long explanations
or blocking multiple lines of code.
*/
let y = 20;
console.log(y);
3️⃣ Commenting Out Code (Disable Code)
You can turn off code temporarily without deleting it.
// console.log("This line is disabled");
console.log("This line will run");
4️⃣ Best Practices for Comments
✅ Use comments to:
- Explain why you wrote something (not what, if it’s obvious).
- Divide sections of code.
- Temporarily test/debug.
❌ Don’t:
- Overuse comments (clean, readable code is better).
- Write obvious things:
let count = 10; // Set count to 10 ❌ (too obvious)
✅ Summary
//
→ Single-line comment/* ... */
→ Multi-line comment- Used for explanations, notes, and disabling code.