What Are Comments in Coding – What They Are and Why They Matter

💬 Concept 2 of 13 · Beginner · ⏱️ 8 min

What Are Comments in Coding?

Notes inside your code that the computer completely ignores — but humans absolutely need.

📖 Definition

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.

📚
Think of it like writing notes in the margin of a textbook. The notes don’t change what the book says — they just help you understand it better when you come back to it later.
⚖️ Code vs Comments

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.

💻 Code
let price = 100;
let discount = 0.2;
let total = price * (1discount);
💻 Tells the computer WHAT to do
💬 Comment
// Apply 20% discount
// for premium members only
// (rule set by marketing team)
💬 Tells humans WHY you did it

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.

🖥️ Computer Behavior

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.

how-computer-reads.js
let name = “Sara”; ✓ Runs
// This stores the user’s name ✗ Skipped
let age = 25; ✓ Runs
// Age must be above 18 to register ✗ Skipped
console.log(name); ✓ Runs

No processing. No errors. No slowdown. The computer simply moves to the next actual line of code.

💡 Comments have zero effect on how your program works. You can write as many comments as you want — the program will run exactly the same way with or without them.
💡

Why Are Comments Important in Programming?

Three reasons every developer — beginner or experienced — needs comments in their code.

🧠 Reason 1

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.

with-comments.js
// Calculate total price after discount
let price = 100;
let discount = 0.2; // 20% off for premium users
let total = price * (1discount);

// 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.

I once spent 45 minutes trying to understand a function I had written myself — because I left no comments. One line of explanation would have saved me all that time.
👥 Reason 2

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.

📋 Real Scenario — Team Project
Without comments: A new developer joins the team and spends 2 days trying to understand what each function does. They are afraid to change anything in case they break something.
With comments: The same developer reads the comments, understands the logic in 30 minutes, and confidently makes the changes they need.

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.

⚡ Reason 3

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.

cart.js
// ── USER LOGIN VALIDATION STARTS HERE ──
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”;
  }
}
💡
The bottom line: Comments don’t just help you understand your code — they help you fix it faster, share it with others more easily, and come back to it months later without starting from scratch.
📝 Types of Comments

Types of Comments in Programming

Three types — each with its own purpose. Learn when to use each one.

Single-Line
📄 Multi-Line
↔️ Inline
➖ Type 1

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.

⚡ JavaScript
🐍 Python
⚡ JavaScript — uses //
// This is a single-line comment
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
🐍 Python — uses #
# This is a single-line comment
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
💡 Best for: Short, quick notes — one thought, one line. If your comment needs more than one line, use a multi-line comment instead.
📄 Type 2

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.

⚡ JavaScript
🐍 Python
⚡ JavaScript — uses /* */
/*
  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 * (1discount);
}
🐍 Python — uses “”” “””
“””
  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 * (1discount)
💡 Best for: Explaining complex logic, documenting functions, or leaving detailed notes at the top of a file. Use when one line is simply not enough.
↔️ Type 3

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.

⚡ JavaScript — inline examples
let discount = 0.2; // 20% off for premium users
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.

⚠️ Keep them short. If your inline comment is getting long, it probably deserves its own line as a single-line comment instead. Inline comments are for quick notes — not full explanations.
📊 Quick Comparison — All 3 Types
TypeSymbol (JS)Symbol (Python)Best Used For
Single-Line//#Short, quick notes on one line
Multi-Line/* */""" """Detailed explanations, function docs
Inline// after code# after codeQuick note on same line as code
🌐

Comments in Different Programming Languages

Same idea, different symbols — here’s how comments look in 4 popular languages.

One of the first things I noticed when switching between languages was that comments look slightly different in each one. The idea is always the same — leave a note for humans — but the symbols change depending on the language you are using. Here is a clear breakdown of all four.
JavaScript ⚡ The Language of the Web //   /* */

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.

⚡ script.js
// Single-line comment in JavaScript
let name = “Sara”; // Inline comment

/*
  Multi-line comment in JavaScript
  Great for longer explanations
*/
function greet() {
  console.log(“Hello!”);
}
Remember: Use // for quick notes and /* */ when you need more than one line.
Python 🐍 Clean and Beginner-Friendly #   “”” “””

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.

🐍 script.py
# Single-line comment in Python
name = “Sara” # Inline comment

“””
  Multi-line comment in Python
  Uses triple quotes instead of /* */
“””
def greet():
  print(“Hello!”)
🐍 Python tip: The # symbol is the most used comment style in Python. Triple quotes are mainly used for documenting functions and classes.
HTML 🌐 The Foundation of Web Pages <!– –>

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.

🌐 index.html
<!– This is a comment in HTML –>
<h1>Hello World</h1>

<!– Navigation section starts here –>
<nav>
  <a href=“/”>Home</a>
</nav>
<!– Navigation section ends here –>
🌐 HTML tip: Use comments to mark the start and end of big sections like header, footer, and navigation. It makes your HTML much easier to read and edit.
CSS 🎨 Styles and Design /* */

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.

🎨 style.css
/* This styles the main heading */
h1 {
  color: blue;
  font-size: 24px; /* size in pixels */
}

/*
  Button styles
  Used on homepage and contact page
*/
button {
  background: #054B8D;
  color: white;
}
🎨 CSS tip: Use comments to group related styles together — like all button styles in one section, all heading styles in another. It saves a lot of searching later.
⚡ Quick Reference — Comment Symbols
JavaScript
// comment
/* multi-line */
🐍
Python
# comment
“”” multi-line “””
🌐
HTML
<!– –>
Single style only
🎨
CSS
/* comment */
Single style only

When Should You Use Comments?

Don’t comment everything — comment the right things.

01
🧠

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.

// Apply discount only if user is premium AND total is above $50
if (isPremium && total > 50) {
  total = total * 0.8;
}
02
📌

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: Add input validation before form submission
// TODO: Handle empty cart edge case
// FIXME: Login fails on mobile — needs investigation
03
📋

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) * (1discount);
}
04
🐛

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.

function getTotal(price) {
  // 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

// Apply discount only for premium users
if (isPremium) {
  price *= 0.8;
}

// TODO: Add error handling here later
fetchData();

// Max 3 login attempts before lockout
let retries = 3;
💡 Explains the why — the reason behind the decision, not just what the code does.

Bad Comment Examples

// Add 1 to i
i++;

// Set name to Sara
let name = “Sara”;

// Loop through the array
for (let item of items) {
  process(item);
}
⚠️ States the obvious — repeats exactly what the code already says. Adds zero value.
💡 Golden Rule: If your comment just repeats what the code says — delete it. A good comment explains why, not what.
📋 Key Takeaways

Key Takeaways

Summary of Comments in Coding

Comments are notes for humans — the computer ignores them completely
Use // for single-line comments in JavaScript, # in Python
Use /* */ for multi-line comments in JavaScript and CSS
Use <!-- --> for comments in HTML
Good comments explain the why — not just what the code does
Comment complex logic, functions, TODO notes, and disabled code
Never comment the obvious — it adds clutter, not value
Always update your comments when you change the code
💡
The one rule to remember: Code tells the computer what to do. Comments tell humans why you did it that way. When both are clear — your code becomes truly professional.

FAQs About Comments in Coding

Quick answers to the questions beginners ask most

Q 01

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.
🌐 index.html
<!– This is an HTML comment — invisible on the page –>
<h1>Hello World</h1>

<!– Navigation starts here –>
<nav></nav>
<!– Navigation ends here –>
💡 HTML comments are great for marking sections of your page — like where the header starts, where the footer begins, and so on.
Q 02

What is an example of a comment in programming?

+
A comment is a note written inside your code that the computer skips entirely. Here is a simple example in both JavaScript and Python:
⚡ JavaScript
// This is a comment — computer ignores this line
let name = “Sara”; // Inline comment

/*
  This is a multi-line comment
  Used for longer explanations
*/
🐍 Python
# This is a comment in Python
name = “Sara” # Inline comment

“””
  This is a multi-line comment in Python
“””
Both examples do the same thing — leave a human-readable note that the computer completely ignores when running the program.
Q 03

Is it i++ or ++i in C?

+
Both i++ and ++i increase the value of i by 1 — but they do it at different times.
⚡ i++ — Post-Increment
i = 5;
print(i++); // prints 5
print(i);   // now i = 6
Uses value first, then adds 1
⚡ ++i — Pre-Increment
i = 5;
print(++i); // prints 6
print(i);   // now i = 6
Adds 1 first, then uses value
For most beginner use cases — like a simple loop counter — both work exactly the same way and the difference does not matter at all.
Still have questions? Browse our free tutorials for clear answers.
Browse Tutorials →