Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Comments in Programming

In programming, comments are text notes in your code that are ignored by the computer.

They can help you and others understand what the code does.

What is a Comment?

A comment is a section in your code that is not executed.

Comments are used to explain, clarify, or comment code for yourself or others who may read it later.

  • Comments make code easier to read and maintain
  • They help document the purpose or logic of code sections
  • They can be used to temporarily disable code for debugging

Why Use Comments?

Good comments make code understandable and maintainable.

Comments can:

  • Explain complex logic
  • Describe the purpose of functions, classes, or files
  • Mark sections that need improvement
  • Make collaboration with others easier
  • Debug code by temporarily disabling lines or blocks

Types of Comments

Most programming languages support several types of comments:

1. Single-line Comments

Used for short explanations or notes.

Either as a whole line:

//this line is a comment
print("Hello");

or at the end of a line:

print("Hello"); //this is a comment

2. Multi-line Comments

Used for longer explanations. Syntax varies by language.

/*This is a comment
that spans over
multiple lines*/
print("Hello");

3. Inline Comments

Comments can also appear inside a line of code. In languages that support multi-line comments, you can place a comment in the middle of a statement.

This is sometimes used to quickly change a value or logic for testing.

firstname = /*"John"*/"Jane";
print("Hello " + firstname);

Note: This technique does not work in Python, as Python does not support block comments inside statements.


Comments in Different Languages

LanguageSingle-lineMulti-lineTry It  
Python# comment""" multi-line
comments """
Try it »
JavaScript// comment/* multi-line
comments */
Try it »
Java// comment/* multi-line
comments */
Try it »
C// comment/* multi-line
comments */
Try it »
C++// comment/* multi-line
comments */
Try it »
HTML<!-- comment --><!-- multi-line
comments -->
Try it »
SQL-- comment/* multi-line
comments */
Try it »
Bash# comment: << 'COMMENT'
multi-line
comments
COMMENT
Try it »

Inline Comments

Inline comments are comments that appear on the same line as code, or even inside a line of code. They are useful for explaining a specific part of a statement, or quickly changing code for testing and debugging.

  • In-line (mid-line) comments: Placed inside a statement.
  • End-of-line inline comments: Placed after a statement to explain its purpose.

Examples

let x = 10;
let y = /*5*/ 7;
let z = x + y;
console.log(z); // outputs 17
int x = 10;
int y = /*5*/ 7;
int z = x + y
System.out.println(y); // Output: 17
int x = 10;
int y = /*5*/ 7;
int z = x + y;
printf("%d\n", y); // Output: 17
int x = 10;
int y = /*5*/ 7;
in z = x + y;
std::cout << y << std::endl; // Output: 17
<p>Hello <!-- John --> Jane!</p> <!-- Outputs 'Hello Jane' -->
SELECT * FROM Customers
WHERE city = /*'London'*/ 'Berlin'; // Returns all customers from Berlin

Using Comments for Debugging

Comments are not just for documentation—they can also be a powerful debugging tool. When troubleshooting code, you can temporarily "comment out" lines or blocks to isolate problems or prevent certain code from running.

  • Comment out code: Disable parts of your code without deleting them, making it easy to restore later.
  • Test changes safely: Try new logic while keeping the original code as comments for reference.
  • Isolate bugs: By commenting out sections, you can narrow down where errors are happening.

Debugging Examples

# Debugging by commenting out a line
x = 10
y = 0
# y = 5
result = x + y
print(result)
// Debugging by commenting out a line
let x = 10;
let y = 0;
// y = 5;
let result = x + y;
console.log(result);
// Debugging by commenting out a line
#include 
using namespace std;
int main() {
  int x = 10;
  int y = 0;
  // y = 5;
  int result = x + y;
  cout << result << endl;
  return 0;
}
// Debugging by commenting out a line
public class Main {
  public static void main(String[] args) {
    int x = 10;
    int y = 0;
    // y = 5;
    int result = x + y;
    System.out.println(result);
  }
}
<!-- Debugging by commenting out a line -->
<!-- <p>This paragraph is temporarily hidden for debugging</p> -->
<p>Hello, world!</p>
-- Debugging by commenting out a line
-- SELECT * FROM Cutomers;
SELECT * FROM Products;
# Debugging by commenting out a line
# rm important_file.txt
echo "Safe for now!"

Tip: Remember to remove or update debugging comments before finalizing your code to keep it clean and maintainable.


Best Practices for Comments

  • Write clear and concise comments
  • Keep comments up-to-date with your code
  • Don't state the obvious (avoid "increment x by 1" for x += 1)
  • Use comments to explain why, not just what
  • Remove outdated or misleading comments

Summary

Comments are a vital tool for making your code readable, maintainable, and collaborative. Use them wisely!


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.