Comment Syntax in Every Major Programming Language
Most languages use //, #, or /* */, and which one you get is almost entirely an accident of ancestry. One reference for every language you’re likely to touch.
Key takeaways
- Comment syntax tracks language family, not language age: C-family languages use // and /* */, shell-derived languages use #, SQL and Haskell use --, and Lisp uses semicolons.
- Python has no true multi-line comment syntax, and triple-quoted strings used as comments are actually string expressions the parser evaluates and discards.
- CSS only supports /* */ comments, and a // line will silently break the rule it appears in, even though Sass and Less accept it.
- Doc-comment conventions like JSDoc, JavaDoc, rustdoc, and godoc matter more than basic comment syntax, because tools turn them into API docs and IDE tooltips for free.
- Assembly is the least consistent case: NASM uses a semicolon, ARM uses # or @, and some MIPS variants use an exclamation mark.
Almost every programming language supports comments, and most of them use one of three formats: // for single-line, /* */ for multi-line, or #for both. The differences map almost perfectly to which language family the language descended from. C-family uses slashes. Shell-family uses hashes. SQL is its own thing. Here's a complete reference.
Plain English
C-Family: // and /* */
The C-family inherited C's comment syntax: single-line with // and multi-line with /* */. This includes:
- C, C++, Objective-C
- Java, Kotlin, Scala, Groovy
- JavaScript, TypeScript
- C#, Dart
- Go, Rust, Swift
- PHP (also supports
#)
// This is a single-line comment
/*
This is a multi-line
comment
*/
/**
* This is a doc comment (JSDoc, JavaDoc, etc.)
* @param x The first argument
*/The /** */ with extra asterisks is a doc-comment convention used by JSDoc, JavaDoc, Doxygen, and similar tools. The syntax is just a regular comment to the compiler, but documentation generators recognize the extra asterisk as a marker for structured doc strings.
Shell and Hash-Family: #
Shell scripts and many high-level languages use # for comments. The convention came from Bourne shell in the 1970s and stuck.
- Python
- Ruby
- Perl
- Bash, sh, zsh, fish
- R
- YAML
- Shell (any flavor)
- Makefiles
# This is a single-line comment
# Python doesn't have multi-line comment syntax,
# you just use multiple single-line comments.
"""
This is technically a string literal,
but it's commonly used as a multi-line
comment inside functions.
"""Python doesn't have true multi-line comments. Triple-quoted strings (""") get used as a stand-in, but they're string expressions the parser evaluates and throws away, not comments. They're also how docstrings work, and those are real: a triple-quoted string right after a def or class is accessible at runtime via __doc__.
SQL: Two Conventions
-- Single-line comment (standard SQL)
# Single-line comment (MySQL, MariaDB only)
/*
Multi-line comment
*/SQL uses -- for single-line, which sometimes confuses developers from C-family backgrounds. MySQL adds # as an alternative single-line. Multi-line is /* */ across most SQL dialects.
HTML and XML
<!-- This is a single-line comment -->
<!--
This is a
multi-line comment
-->Same syntax for both single and multi-line. The opening <!-- and closing --> work for any number of lines. Note that you can't nest HTML comments, and the string -- inside a comment is technically illegal per the spec (browsers tolerate it).
CSS
/* This is a comment */
/* CSS only has multi-line style.
No single-line // syntax. */CSS uses /* */ only. The // single-line comment is not valid CSS and will silently break the rule it appears in. CSS preprocessors (Sass, Less) do support //, but plain CSS doesn't.
Lisp Family: ;
; This is a single-line comment
;; Two semicolons for top-level comments by convention
;;; Three semicolons for file-level comments
#| This is
a multi-line comment |#Lisp-family languages use ; for single-line and #| ... |# for multi-line. The number of semicolons is a stylistic convention indicating comment level.
Haskell and ML-Family
-- This is a single-line comment
{-
This is a
multi-line comment
-}Haskell uses -- for single-line (same as SQL) and {- -} for multi-line. OCaml uses (* *), which is Pascal's convention.
Assembly
Different assemblers use different conventions:
;for x86 NASM and most Intel-syntax assemblers#or@for ARM!for some MIPS variants;or--for various others
Assembly is the wild west of comment syntax. Read the specific assembler's docs.
The Languages That Don't Fit
A few worth noting:
- Fortran:
!for end-of-line comments (modern Fortran). Older Fortran used aCin the first column. - Erlang, Elixir:
%for Erlang,#for Elixir. - VB / VBA:
'(single quote) orREM. - Pascal / Delphi:
{ }or(* *). - BASIC:
REMor'.
Doc Comments
Most languages have a convention for documentation comments that get parsed by tools:
- Java:
/** */(JavaDoc) - JavaScript/TypeScript:
/** */(JSDoc, TSDoc) - C++:
/** */or///(Doxygen) - Python: triple-quoted string immediately after a function or class (docstring)
- Rust:
///(rustdoc) - Go: regular
//comments immediately above a declaration (godoc) - Ruby: comments above the method, processed by RDoc/YARD
These conventions matter because many tools generate API documentation from them, and good doc comments end up rendered into HTML pages, IDE tooltips, or man pages without you doing anything extra.
Takeaway
Most languages use //, #, or some variant for comments. The pattern is largely inherited from the language family the language descended from: C-style for compiled languages, # for shell-derived, -- for SQL and Haskell. When in doubt, check the language docs. Doc-comment conventions are usually more important than the basic syntax for any code that'll be read by other people.
The Take
Comment syntax is one of those things you absorb in the first week of a new language and never think about again. The interesting part is the doc-comment convention, because that's what determines whether your code generates useful documentation or not. For any function you expect someone else to call, write a doc comment in the language's standard format. The IDE tooltips alone justify the time, and the generated docs become real artifacts that outlast the team.
Frequently asked questions
- What is the most common comment syntax across programming languages?
- Three formats cover almost everything: // for single-line, /* */ for multi-line, and # for single-line in shell-derived languages. Which one a language uses depends on the family it descended from. C, Java, JavaScript, Go, Rust, and Swift all inherited C-style slashes. Python, Ruby, Perl, Bash, R, and YAML inherited the hash from Bourne shell.
- Does Python have multi-line comments?
- No, Python has no true multi-line comment syntax. You just stack # lines. Triple-quoted strings are often used as a stand-in, but they're string expressions the parser evaluates and throws away, not comments. The one place they're real is docstrings: a triple-quoted string right after a function or class definition is accessible at runtime via __doc__.
- Can you use // for comments in CSS?
- No, // is not valid CSS and will silently break the rule it appears in. Plain CSS only supports /* */ style comments, for both single-line and multi-line. CSS preprocessors like Sass and Less do accept //, which is where the confusion comes from. If you're writing raw CSS, stick with /* */.
- What are doc comments and why do they matter?
- Doc comments are comments written in a format that documentation tools parse into structured API docs. Java and JavaScript use /** */, Rust uses ///, Go uses regular // comments directly above a declaration, and Python uses docstrings. They matter because the IDE tooltips and generated doc pages come free once you write them, and those artifacts outlast the team.
Written by
Tech Talk News Editorial
Computer engineering background. Writes about software, AI, markets, and real estate, and the places where the three meet.
More about the author