A comment is text in your code that the computer ignores when the program runs. It’s meant for humans, not the computer.
What a comment does#
Consider this example:
Console.WriteLine("Hello, World!");
// This is a comment. It's ignored and not executed.
Console.WriteLine("Hello, Again!");print("Hello, World!")
# This is a comment. It's ignored and not executed.
print("Hello, Again!")console.log("Hello, World!");
// This is a comment. It's ignored and not executed.
console.log("Hello, Again!");The output of this program is:
Hello, World!
Hello, Again!
The comment has no effect — the program runs exactly as if the comment line weren’t there.
When to use comments#
Comments are useful for explaining code that isn’t immediately clear. They help others understand your code, and they help you when you come back to it later.
Use comments when they add clarity. Avoid them when the code is already obvious.
How to write comments#
Comment syntax differs by language. Each language has its own page with the full syntax details:
Common Mistakes#
Writing comments that restate the code
// add 1 to score above score = score + 1 adds no value — the code already says that. Comments should explain why, not what. Save them for decisions that aren’t obvious from reading the code.
Forgetting to update comments when code changes A comment that describes what the code used to do is worse than no comment. If you change the code, check whether any nearby comments need updating too.
Using comments to hide broken code Commenting out code you’re not sure about is fine temporarily, but leaving dead commented-out code in a file makes it harder to read. Clean it up once you’ve decided whether you need it.