What Are Comments in Coding?
Notes inside your code that the computer completely ignores — but humans absolutely need.
Simple Definition of Comments for Beginners
I remember the first time I saw // in someone else’s code. I had no idea what it was. I thought it was part of the actual program — something the computer needed. Turns out, the computer ignores it completely. That was my introduction to comments.
A comment is a line — or a few lines — that you write inside your code, but the computer never reads it.
Comments are written by developers, for developers. They are notes, reminders, and explanations sitting right next to the actual code — completely invisible to the computer, but extremely useful for any human reading the code.
Why Comments Exist — Code vs Comments
Code tells the computer what to do. Comments tell humans why you did it that way.
Those are two very different things — and both matter.
let discount = 0.2;
let total = price * (1 – discount);
A computer does not care why you wrote something. It just runs the instructions. But when you come back to your own code two weeks later, you will absolutely care why you made certain decisions. Without comments, even your own code can feel like a mystery.
How the Computer Treats Comments
When your program runs, the computer reads through your code line by line. The moment it sees a comment — it skips it entirely.
No processing. No errors. No slowdown. The computer simply moves to the next actual line of code.
Why Are Comments Important in Programming?
Three reasons every developer — beginner or experienced — needs comments in their code.
Comments Help You Remember What Your Code Does
Most beginners think comments are optional — something you add when you feel like it. I thought the same thing. Then I opened a project I had written three weeks earlier and could not understand a single line of it. That was the day I started taking comments seriously.
Code that makes perfect sense today will confuse you tomorrow. When you write code, everything is fresh in your mind — the logic, the decisions, the reasons. But come back two weeks later, and it feels like reading someone else’s work.
let price = 100;
let discount = 0.2; // 20% off for premium users
let total = price * (1 – discount);
// Show final amount to user
console.log(“Your total: $” + total);
Comments act as a memory backup. A short note like // calculate total price after discount tells you exactly what this block does — without having to re-read every line to figure it out.
Comments Make Code Easier for Others to Read
Coding is rarely a solo activity. In real projects, teams of developers work on the same codebase. Someone else will read your code — and they will not know what was going on in your head when you wrote it.
Good comments bridge the gap between your private thought process and shared team knowledge. Even if you are working alone right now — the “someone else” reading your code might just be you, six months from now, with no memory of what you wrote.
Comments Save Time When Fixing Problems
When something breaks in your code — and it will, that’s just part of coding — you need to find the problem fast.
Comments act like a map
Instead of reading hundreds of lines, comments guide you directly to the section you need to fix.
Find the problem faster
A comment like // user login validation starts here tells you exactly where to look without guessing.
Save hours of debugging time
Well-commented code cuts debugging time significantly — especially on large projects with many files.
function validateUser(email, password) {
// Check if email format is correct
if (!email.includes(“@”)) {
return “Invalid email”;
}
// Check minimum password length
if (password.length < 8) {
return “Password too short”;
}
}
Types of Comments in Programming
Three types — each with its own purpose. Learn when to use each one.
Single-Line Comments
A single-line comment is exactly what it sounds like — one line of explanation. It starts with a special symbol, and everything after that symbol on the same line becomes a comment. The computer skips it completely.
Single-line comments are the most common type you will see. They are quick, clean, and perfect for short explanations.
let name = “Sara”;
// Comments can explain what a line does
let age = 25;
// You can also use them to remind yourself of things
// TODO: Add error handling here later
name = “Sara”
# Comments can explain what a line does
age = 25
# You can also use them to remind yourself of things
# TODO: Add error handling here later
Multi-Line Comments
Sometimes one line is not enough. When you need to explain something in more detail — like a complex function, a section of code, or who wrote the file — that’s when multi-line comments come in.
A multi-line comment lets you write as many lines as you need, all inside a single comment block.
This function calculates the total price.
It takes the base price and applies a discount.
Written by: Ahmed — March 2025
*/
function calculateTotal(price, discount) {
return price * (1 – discount);
}
This function calculates the total price.
It takes the base price and applies a discount.
Written by: Ahmed — March 2025
“””
def calculate_total(price, discount):
return price * (1 – discount)
Inline Comments
An inline comment sits on the same line as your code — right after the actual instruction. It is the shortest type of comment, usually just a few words, used to quickly explain what a specific line is doing.
let maxItems = 10; // cart limit set by business team
let taxRate = 0.08; // 8% tax — required by law
let retries = 3; // max login attempts before lockout
Inline comments are useful when the code itself is almost clear, but one small detail needs a quick explanation.
| Type | Symbol (JS) | Symbol (Python) | Best Used For |
|---|---|---|---|
| Single-Line | // | # | Short, quick notes on one line |
| Multi-Line | /* */ | """ """ | Detailed explanations, function docs |
| Inline | // after code | # after code | Quick note on same line as code |
Comments in Different Programming Languages
Same idea, different symbols — here’s how comments look in 4 popular languages.
JavaScript uses // for single-line comments and /* */ for multi-line comments. You will see these everywhere in web development — they are the most common comment style beginners encounter first.
let name = “Sara”; // Inline comment
/*
Multi-line comment in JavaScript
Great for longer explanations
*/
function greet() {
console.log(“Hello!”);
}
// for quick notes and /* */ when you need more than one line.Python uses the # symbol for single-line comments. For multi-line comments, Python developers use triple quotes “””. Python’s comment style is clean and minimal — which fits perfectly with how Python code looks in general.
name = “Sara” # Inline comment
“””
Multi-line comment in Python
Uses triple quotes instead of /* */
“””
def greet():
print(“Hello!”)
# symbol is the most used comment style in Python. Triple quotes are mainly used for documenting functions and classes.HTML uses a completely different style. Comments in HTML start with <!– and end with –>. Everything between those symbols is a comment. HTML comments are especially useful for marking sections of your page layout so you know what each part does.
<h1>Hello World</h1>
<!– Navigation section starts here –>
<nav>
<a href=“/”>Home</a>
</nav>
<!– Navigation section ends here –>
CSS uses the same /* */ style as JavaScript multi-line comments. There is no single-line comment style in CSS — /* */ is the only option, but it works perfectly fine for both short and long comments.
h1 {
color: blue;
font-size: 24px; /* size in pixels */
}
/*
Button styles
Used on homepage and contact page
*/
button {
background: #054B8D;
color: white;
}
When Should You Use Comments?
Don’t comment everything — comment the right things.
Explaining Complex Logic
If you look at a piece of code and think “this might confuse someone” — write a comment. Complex conditions, tricky calculations, or unusual logic all deserve a short explanation. That someone might be a teammate — or you, three weeks from now.
if (isPremium && total > 50) {
total = total * 0.8;
}
Marking TODO Notes
When you know something needs to be fixed or added later, mark it clearly with a TODO comment so it does not get forgotten. This is one of the most practical uses of comments in real development.
// TODO: Handle empty cart edge case
// FIXME: Login fails on mobile — needs investigation
Documenting Functions
Before a function, a short comment explaining what it does, what it expects, and what it returns saves enormous time — especially when the project grows larger.
calculateTotal — adds items and applies discount
Expects: items (array), discount (number 0–1)
Returns: final price after discount
*/
function calculateTotal(items, discount) {
return items.reduce((sum, item) => sum + item.price, 0) * (1 – discount);
}
Temporarily Disabling Code While Debugging
Sometimes you need to test your code with certain lines turned off. Instead of deleting them, comment them out. When debugging is done, simply uncomment them and they are back.
// Temporarily disabled while debugging
// price = price * 0.9;
return price;
}
Good vs Bad Comment Examples
Not all comments are helpful — here’s the difference.
Good Comment Examples
if (isPremium) {
price *= 0.8;
}
// TODO: Add error handling here later
fetchData();
// Max 3 login attempts before lockout
let retries = 3;
Bad Comment Examples
i++;
// Set name to Sara
let name = “Sara”;
// Loop through the array
for (let item of items) {
process(item);
}
Key Takeaways
Summary of Comments in Coding
// for single-line comments in JavaScript, # in Python/* */ for multi-line comments in JavaScript and CSS<!-- --> for comments in HTMLFAQs About Comments in Coding
Quick answers to the questions beginners ask most
What is <!– –> in HTML?
<!-- and --> are the comment symbols in HTML. Everything written between these two symbols becomes a comment — the browser ignores it completely and it never shows up on the actual web page.<h1>Hello World</h1>
<!– Navigation starts here –>
<nav>…</nav>
<!– Navigation ends here –>
What is an example of a comment in programming?
let name = “Sara”; // Inline comment
/*
This is a multi-line comment
Used for longer explanations
*/
name = “Sara” # Inline comment
“””
This is a multi-line comment in Python
“””
Is it i++ or ++i in C?
i by 1 — but they do it at different times.print(i++); // prints 5
print(i); // now i = 6
print(++i); // prints 6
print(i); // now i = 6

// for premium members only
// (rule set by marketing team)