Articles
Published · November 18, 2026

Comment Syntax Across Languages: //, #, and /* */ in Snippets

Line comments use // or #, block comments use /* */. A quick map of comment markers by language, plus the nesting and pasting traps that break saved snippets.

Also in: Español中文हिन्दी

Most languages give you a line comment that ends where the line ends and a block comment with a start and end marker. The markers differ by language, so a snippet saved for one language often breaks when you paste it into another. Here's a short map of comment syntax across programming languages, and the traps that come with it.

Quick answer

Most languages support block comments, line comments, or both). A line comment ends at the end of the text line. A block comment is delimited by start and end text and can span lines or sit in the middle of one.

| Family | Line marker | Block marker | Languages | | --- | --- | --- | --- | | C style | // | /* */ | C++, Java, JavaScript, Go, Swift, Rust | | Hash | # | none (Ruby has =begin and =end) | Python, Ruby, Perl, shell | | Dashes | -- | varies (--[[ ]] in Lua, {- -} in Haskell) | SQL, Lua, Haskell | | Markup | none | <!-- --> in HTML, /* */ in CSS | HTML, CSS |

If you remember one rule, make it this: check the language before you comment anything out.

The // and /* */ family

C, C++ and their relatives use /* */ for blocks and // for lines, and JavaScript follows the same style.

// a line comment: everything after the slashes is ignored
const total = price * qty; // it can also trail code

/* a block comment
   can span several lines */
const tax = total * 0.08;

MDN describes the // comment as making all text after it on the same line a comment, and /* */ as the more flexible form, usable even in the middle of a line. It also notes that a block comment containing a line break behaves like a line break for automatic semicolon insertion. So a pasted comment can quietly change how code is read.

CSS has only the block form, so a // line in a stylesheet is not a comment.

Nesting is where block comments bite

A block comment ends at the first closing marker. In C and C++ that means you cannot wrap a chunk of code that already contains a /* */ comment:

/* disabled for now
int a = 1; /* old note */
int b = 2;
*/

The comment ends at the first */. The stray int b = 2; and */ become code, and the compiler complains. Some languages allow nesting, such as D and Swift, while C and C++ do not). The comparison tables also mark Rust and Haskell blocks as nestable. When you are unsure, put // in front of each line instead.

The # family and the lines that are not comments

In Python, a comment starts with a hash character that is not part of a string literal and ends at the end of the physical line. Python has no block comment syntax. People often use a triple-quoted string on its own line as a stand-in, but that is a string literal, not a comment. For a multi-line note, use a # on each line.

# first line of the note
# second line of the note
total = price * qty  # trailing comment

Ruby, Perl and shell scripts also use # for lines. A # is not always an ordinary comment, though:

Delete either line as "just a comment" and behavior can change.

Markers outside the two big families

SQL, Lua and Haskell use `--` for line comments). HTML uses <!-- --> and has no line form. A snippet that starts with -- is probably SQL, and one that starts with // is not.

-- list the ten newest notes
SELECT title FROM notes ORDER BY created_at DESC LIMIT 10;

Some formats have no comments at all. JSON has no comments, so a // line inside a JSON snippet makes the file invalid unless a tool accepts a variant.

Commenting out code in a snippet

Commenting out means adding comment syntax so executable code no longer runs, which is often used to track down a bug. A few habits keep it safe and boring:

  1. Identify the language first, then use its marker.
  2. Prefer a line marker on every line over a block marker when the code may already hold a block comment.
  3. Keep the comment marker flush with the code so the diff stays readable.
  4. When you paste a snippet into another language, replace the markers, not just the code.

How Carets fits in

Comment markers only help when you know which language you're looking at. Carets is a native notes and code editor for iPhone, iPad, and Mac that keeps Markdown, plain text, and code in one place, with syntax highlighting for code and config files. Set a snippet's language and its comments are colored as comments, so a # that Python ignores looks different from one in a string. Snippets sit in files, projects, and tags, and stay plain text, so they remain portable. If you build snippet libraries, see snippet tabstops and placeholders too.

Get Carets on the App Store for iPhone, iPad, and Mac.

Frequently Asked Questions

Does Python have block comments?

No. Python's reference defines only the # comment, which runs to the end of the physical line. A triple-quoted string left on its own line is a string literal, not a comment, so use # on each line instead.

Can I nest /* */ comments?

Not in C or C++: the first closing marker ends the comment. Rust, Swift and Haskell allow nesting. Check the language before commenting out a block that already contains a block comment.

Why did my pasted snippet break after I commented it out?

Usually the marker belongs to another language, or the code already holds a comment that ends your block early. Switch to the marker for the snippet's language.

Is a # line always a comment?

Not always. A #! line at the very top of a JavaScript file or shell script is a hashbang, and a Python comment in the first two lines can act as an encoding declaration.