Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.

Comments let you leave notes in source code without treating ordinary explanatory text as program instructions. The markers vary by language: many use // or /* ... */, while Python, Ruby, R, and Bash commonly use #. This reference shows the basic syntax in 15 languages and flags important exceptions—especially docstrings, documentation comments, and languages with no native block-comment form.

Comment syntax at a glance

In ordinary source code, a comment is text the language processor does not treat as executable code. Tools may still read or transform comments—for example, to generate documentation or process directives—so “ignored” does not mean every tool ignores them. The examples below are syntax references, not style rules.

Language Single-line Multiline or block Documentation form or caveat
Python # No general block delimiter Docstrings are string literals, not comments.
JavaScript // /* ... */ /** ... */ is commonly used by JSDoc tooling.
Java // /* ... */ /** ... */ is a Javadoc comment.
C // (C99 and later) /* ... */ Block comments do not nest; use block syntax for older-dialect portability.
C++ // /* ... */ Ordinary block comments do not nest.
C# // /* ... */ /// starts XML documentation comments.
Go // /* ... */ Comments before declarations can be documentation; tool directives are distinct.
Rust // /* ... */ Supports nested blocks and several documentation-comment forms.
PHP // or # /* ... */ Comment boundaries can matter when PHP is embedded in HTML.
Ruby # =begin … =end The block form has placement rules; repeated # lines are common.
Swift // /* ... */ Balanced multiline comments can nest.
Kotlin // /* ... */ Blocks can nest; /** ... */ is used for KDoc.
R # No native block delimiter Use a # on each line.
SQL -- /* ... */ Details can vary across databases and client tools.
Bash # No ordinary block delimiter Here-documents are workarounds, not native block comments.

Examples in 15 languages

1. Python

# This is a single-line comment

# A longer explanation can use
# one comment marker on each line.

def greet(name):
    """Return a greeting for name."""
    return f"Hello, {name}!"

Python has no dedicated multiline-comment delimiter. The triple-quoted text is a docstring: a string literal associated with the function, which can be available through greet.__doc__. It is not a lexical comment. See the Python lexical reference and its section on documentation strings.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

2. JavaScript

// This is a single-line comment

/*
  This is a multiline comment.
*/

const total = 2 + 2; // An inline comment

A comment can follow code on the same line when it is outside a string or other token. A leading /** ... */ is commonly used for JSDoc, but JavaScript itself does not turn it into API documentation; that behavior comes from documentation tools and conventions. See the JavaScript lexical grammar.

3. Java

// This is a single-line comment

/*
  This is a multiline comment.
*/

/**
 * Represents a user account.
 */
class UserAccount {
}

/** ... */ is a Javadoc documentation comment that the javadoc tool can use to produce API documentation; it is not just a differently decorated ordinary comment. See the Javadoc guide and the Java language specification.

4. C

// Available in C99 and later

/* This block form is portable across older C dialects too. */

int total = 2 + 2;

// is standard beginning with C99; a compiler operating in an older dialect may not accept it. For maximum compatibility with older C, use /* ... */. C block comments cannot nest: the first */ closes the comment. See Microsoft’s C comment reference for the documented forms and nesting limitation.

5. C++

// This is a single-line comment

/*
  This is a multiline comment.
*/

int total = 2 + 2;

C++ supports both forms, but ordinary block comments do not nest. Do not put an apparent /* ... */ comment inside another block and expect it to behave as a nested comment: the first closing marker ends the block. See Microsoft’s C++ comments reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

6. C#

// This is a single-line comment

/*
  This is a multiline comment.
*/

int total = 2 + 2;

/// <summary>
/// Adds two integers.
/// </summary>
int Add(int left, int right) => left + right;

The three-slash form supplies XML documentation input for tooling; it is distinct from an ordinary // note. Comments can also appear between parts of an expression, as in left /* first operand */ + right. See the C# language reference.

7. Go

// This is a single-line comment

/*
  This is a multiline comment.
*/

// Add returns the sum of left and right.
func Add(left, right int) int {
	return left + right
}

A comment immediately before a top-level declaration can serve as its documentation for Go’s documentation tools. Comments such as //go:generate are tool directives, not merely prose; do not assume every comment is semantically inert to the wider toolchain. See Go’s documentation-comment guidance and the language specification.

8. Rust

// This is a single-line comment

/*
  This is a multiline comment.
*/

/// Adds two integers.
fn add(left: i32, right: i32) -> i32 {
    left + right
}

//! Documentation for the enclosing module.

Rust supports nested block comments. Its documentation forms include /// and /** ... */ for the following item, plus //! and /*! ... */ for the enclosing item or module. See the Rust Reference.

9. PHP

<?php
// This is a single-line comment
# This is also a single-line comment

/*
  This is a multiline comment.
*/

$total = 2 + 2;

PHP supports C-style, C++-style, and shell-style comments. In files that mix PHP and HTML, a single-line comment ends at the line ending or the end of the current PHP block, so pay attention to where PHP code begins and ends. See the PHP manual.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

10. Ruby

# This is a single-line comment

=begin
This is a multiline comment.
=end

total = 2 + 2

=begin and =end have placement requirements and are less flexible than ordinary comments. For everyday multiline notes, repeated # lines are often clearer:

# This is a multiline comment
# written as several single-line comments.

See the Ruby syntax reference.

11. Swift

// This is a single-line comment

/*
  This is a multiline comment.
*/

let total = 2 + 2

Unlike C and C++, Swift permits nested multiline comments when each opening and closing marker is balanced. That can be useful when temporarily commenting out a section that already contains a block comment. See Swift’s lexical structure reference.

12. Kotlin

// This is a single-line comment

/*
  This is a multiline comment.
*/

val total = 2 + 2

/**
 * Adds two integers.
 */
fun add(left: Int, right: Int): Int = left + right

Kotlin block comments can nest. The /** ... */ form is conventionally used for KDoc, which Kotlin documentation tools can process; it is not a Python-style runtime docstring. See the Kotlin documentation on KDoc.

13. R

# This is a single-line comment

# This is a multiline comment
# written using multiple single-line comments.

total <- 2 + 2

R has no native /* ... */ block-comment syntax. Put # at the start of each explanatory line. See the R language manual.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

14. SQL

-- This is a single-line comment

/*
  This is a multiline comment.
*/

SELECT 2 + 2;

These are common SQL comment forms, but SQL is implemented by different database systems and client tools; details and restrictions can differ. Oracle documents both forms and notes a SQL*Plus-specific restriction involving blank lines in block comments. SQL’s COMMENT statement, where supported, attaches metadata to database objects—it is not a source-code comment. See Oracle’s SQL comments documentation.

15. Bash

#!/usr/bin/env bash

# This is a single-line comment

# For a longer note, repeat the marker.
# This keeps each line an ordinary shell comment.

total=$((2 + 2))

Bash has no ordinary multiline-comment delimiter. A here-document directed to the no-op command is sometimes used to suppress a block, but it is a shell construct—not a comment—and the shell still parses it:

: <<'COMMENT'
This text is supplied to the no-op command.
COMMENT

For normal explanations or temporarily disabled lines, repeated # comments are safer and clearer. See the Bash manual.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Comments, documentation, and directives are not the same thing

  • Ordinary comments are lexical notes, such as // in JavaScript or # in Python. The language parser does not execute their prose as code.
  • Docstrings are string literals used as documentation. Python’s triple-quoted function example is stored as a string and can be inspected at runtime; it is not a comment.
  • Documentation comments are comments that documentation tools recognize. Examples include Java’s Javadoc, C#’s ///, Rust’s /// and //!, and Go comments placed before declarations. JavaScript’s JSDoc syntax is a tooling convention rather than a separate JavaScript language feature.
  • Directives and pragmas look like comments in some languages but tell tools to do something. Go’s //go:generate is one example.

HTML uses <!-- ... -->, but HTML is a markup language rather than one of the 15 programming languages here. HTML comments also have their own placement rules; they are not interchangeable with JavaScript or CSS comments. See MDN’s HTML comments guide.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Common mistakes and safer ways to disable code

  • Using a delimiter from another language: # is normal in Python, Ruby, R, and Bash, but not a universal marker. SQL commonly uses --; JavaScript and C# do not.
  • Leaving a block unclosed: A missing */ can make the rest of a file appear to be inside a comment or trigger a misleading syntax error later in the file.
  • Nesting a non-nesting block: In C and C++, the first */ closes the block. Rust, Swift, and Kotlin support nested blocks, but that behavior is language-specific.
  • Wrapping code that already contains block comments: A new /* ... */ around the old code can close at an inner */. For a short section, use the editor’s toggle-line-comment command or comment lines individually. Remove dead code that no longer serves a purpose.
  • Assuming comments can appear inside any token: A comment generally acts like whitespace; it cannot safely be inserted arbitrarily inside an identifier, number, or operator.
  • Treating comments as secret storage: Comments can remain in repositories, backups, generated files, package archives, or browser-delivered source. Never put passwords, API keys, private URLs, or personal data in them.

Practical commenting habits

  • Explain why a non-obvious decision exists, rather than narrating what the next line plainly does.
  • Keep a comment close to the code it describes, and update or remove it when behavior changes.
  • Use the language and project conventions for public API documentation comments.
  • Delete obsolete TODOs and stale workaround notes; use an issue tracker or version-control history for context that belongs outside the current code.
  • Remember that comments may be consumed by documentation generators, preprocessors, linters, formatters, IDEs, minifiers, or other build tools even when the language runtime ignores ordinary prose.

Quick copy-and-paste reference

Language Common comment marker(s)
Python #
JavaScript, Java, C++, C#, Go, Rust, Swift, Kotlin // and /* ... */
C /* ... */ for older-dialect portability; // in C99 and later
PHP //, #, /* ... */
Ruby, R, Bash #
SQL -- and commonly /* ... */

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.