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.
| Language | Line | Block | Notes |
|---|---|---|---|
| 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 | # | None | Triple quoted strings are string literals, not comments. See the edge cases guide. |
| Ruby | # | =begin =end | Both markers must be at the start of a line. |
| Perl | # | =pod to =cut | POD 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 batch | REM 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 REM | None | |
| Fortran | ! | None | In 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
| Language | Line | Block | Notes |
|---|---|---|---|
| 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. |
| HTML | None | <!-- --> | Code inside <script> and <style> follows JavaScript and CSS rules. |
| XML | None | <!-- --> | Two dashes in a row are not allowed inside a comment. |
| CSS | None | /* */ | // is not a comment in plain CSS. |
| SCSS, Less | // | /* */ | // comments are left out of the compiled CSS. |
| Markdown | None | <!-- --> | HTML comments are hidden by most Markdown renderers. |
| JSX | None | {/* */} | Inside markup. Normal JavaScript comments work outside it. |
| Razor (.cshtml) | None | @* *@ | |
| Blade | None | {{-- --}} | Not included in the rendered HTML. |
| Twig, Jinja | None | {# #} | |
| Handlebars | None | {{! }} or {{!-- --}} | Use the long form if the comment contains }}. |
Configuration and build files
| Format | Comment | Notes |
|---|---|---|
| JSON | None | Standard 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:
- Shebang lines such as
#!/usr/bin/env python3. - Encoding declarations such as
# -*- coding: utf-8 -*-in Python. - Build directives such as
//go:build linuxin Go. - Linter and type checker controls such as
// @ts-expect-error,// eslint-disable-next-line,# noqaand# type: ignore. - Database hints such as MySQL
/*! ... */and Oracle/*+ INDEX(t idx) */. - License identifiers such as
SPDX-License-Identifier:, which licensing tools scan for. - Documentation comments such as
///in C# and/** */in Java.
To strip comments from any of these languages, Remove Comments lists which preset to start from.