Comment syntax by language

How comments are written in common programming languages, SQL dialects, markup, templates and configuration files, with notes on the cases that surprise people.

Programming languages

A line comment runs to the end of the line. A block comment runs from its opening marker to its closing marker. "Nest" means a block comment can contain another one, which matters when you comment out code that already has comments.

LanguageLineBlockNotes
C///* */// is standard since C99. Block comments do not nest.
C++///* */Block comments do not nest.
C#///* *//// starts an XML documentation comment. #region and #pragma are preprocessor directives, not comments.
Java///* *//** */ is a Javadoc comment.
JavaScript, TypeScript///* *//** */ is JSDoc. A #! line at the very top is allowed. TypeScript reads /// <reference ... /> as a directive.
Go///* *///go:build and //go:generate are directives.
Rust///* */Block comments nest. /// and //! are documentation comments.
Swift///* */Block comments nest. /// is a documentation comment.
Kotlin///* */Block comments nest. /** */ is KDoc.
Dart///* */Block comments nest. /// is a documentation comment.
Scala///* */Block comments nest.
PHP// or #/* */Since PHP 8, #[ starts an attribute, not a comment. A line comment also ends at ?>.
Python#NoneTriple quoted strings are string literals, not comments. See the edge cases guide.
Ruby#=begin =endBoth markers must be at the start of a line.
Perl#=pod to =cutPOD blocks are documentation that the interpreter skips.
Bash, sh#None# starts a comment only at the start of a word, so echo a#b prints a#b. Line 1 may be a #! shebang.
PowerShell#<# #>Block comments are also used for comment based help such as .SYNOPSIS.
Windows batchREM or ::None:: inside a parenthesized block can cause errors. Use REM there.
Lua----[[ ]]--[==[ ]==] with equal signs lets the comment contain ]].
Haskell--{- -}Block comments nest.
R#None
MATLAB%%{ %}Each block marker must be alone on its line. Octave also accepts #.
Visual Basic, VBA' or REMNone
Fortran!NoneIn fixed form source, C or * in column 1 also marks a comment.
Common Lisp;#| |#Block comments nest.
Clojure;None#_ skips the next form. (comment ...) is often used for blocks.

Query, markup and style languages

LanguageLineBlockNotes
SQL (standard)--/* */
MySQL, MariaDB-- or #/* */-- must be followed by a space or control character. /*! ... */ is an executable comment that MySQL runs.
PostgreSQL--/* */Block comments nest.
SQL Server (T-SQL)--/* */Block comments nest.
Oracle--/* *//*+ ... */ is an optimizer hint, not a plain comment.
HTMLNone<!-- -->Code inside <script> and <style> follows JavaScript and CSS rules.
XMLNone<!-- -->Two dashes in a row are not allowed inside a comment.
CSSNone/* */// is not a comment in plain CSS.
SCSS, Less///* */// comments are left out of the compiled CSS.
MarkdownNone<!-- -->HTML comments are hidden by most Markdown renderers.
JSXNone{/* */}Inside markup. Normal JavaScript comments work outside it.
Razor (.cshtml)None@* *@
BladeNone{{-- --}}Not included in the rendered HTML.
Twig, JinjaNone{# #}
HandlebarsNone{{! }} or {{!-- --}}Use the long form if the comment contains }}.

Configuration and build files

FormatCommentNotes
JSONNoneStandard JSON has no comments. JSONC, used by VS Code settings and tsconfig.json, and JSON5 accept // and /* */.
YAML#When # follows a value, it needs a space before it. url: http://x/#top keeps the #top.
TOML#
INI; and often #Support for # and for comments after a value depends on the parser.
Dockerfile## syntax= and # escape= at the top of the file are parser directives.
Makefile#
.gitignore, .gitattributes#Only at the start of a line.

Comments that are really instructions

Some text looks like a comment but is read by a compiler, interpreter or tool. Removing it changes how the code runs or is checked:

To strip comments from any of these languages, Remove Comments lists which preset to start from.