icon

↓ Blogs

Comments in JavaScript - Why & How to Use Them

As a beginner, your code will grow — and so will your confusion if you don’t leave clues. Comments help you explain what your code does, why it’s written that way, or what to fix later
Think of comments as sticky notes for your future self (and your teammates).
✍️ What is a Comment?
A comment is ignored by JavaScript when the code runs. It’s only there to help humans understand your code.
There are two types:
1. Single-line comment:
// This is a single-line comment
let score = 10; // score starts at 10
2. Multi-line comment:
/* This is a
   multi-line comment */
let name = "Harsh";
🧠 Why Use Comments?
1. 🧠 To explain complex logic
2. ✅ To describe what each section does
3. 🛠️ To leave reminders like // TODO: optimize later
4. 🔍 To quickly disable code without deleting it
Example:
// Calculate the final price with tax
let price = 100;
let tax = 18;
let total = price + tax;
⚠️ When Not to Overuse
Don’t write obvious or unnecessary comments:
let x = 5; // declare x as 5 ❌ (we can already see that!)
🧩 Best Practices
1. ✨ Keep comments short and meaningful
2. 📍 Use // TODO: and // NOTE: when you’re still working on something
3. ✏️ Comment why you’re doing something, not just what
🔧 Quick Tip
💬 Comments should guide, not clutter. If your code is self-explanatory, you don’t need to comment every line.
🧪 Practice Challenge
Try this:
// Declare user's name
let userName = "Riya";
// TODO: Add age field here
Now add a few more lines of code and use both single-line and multi-line comments to describe them.
👋 Sign-Off
Learning to write code is step one.
Learning to explain your code — even to yourself — is what sets great developers apart.
Tomorrow, we’ll explore Operators — the math and logic tools of JavaScript.

Want to work together?

Get answers and advice from people you want it from. Techsphere designers and developers will help you create awesome softwares based on your requirements.