JavaScript has two kinds of comments: single-line comments that start with //, and multi-line comments wrapped in /* ... */. The engine ignores everything inside a comment, so you can use them to explain code, leave notes, or temporarily disable lines. These javascript comments cost nothing at runtime and are one of the easiest ways to make code easier to read.
Comments are written for people, not for the computer. Good ones explain why the code does something, not what each line obviously does.
Everything from // to the end of the line is a comment. You can put one on its own line or after code:
The comment after taxRate does not affect the value. Single-line comments are the most common kind in modern JavaScript, and many style guides recommend using them for everything except documentation blocks.
A block comment starts with /* and ends with */. It can span any number of lines:
Block comments cannot be nested. This breaks, because the first */ ends the whole comment:
The text still outside */ is then parsed as code and causes a syntax error. If you need to comment out code that already contains a block comment, use // on each line instead (most editors do this with Ctrl+/ or Cmd+/).
You can disable a line temporarily by turning it into a comment:
This is useful while experimenting, but do not commit large blocks of dead code. Version control (Git) already remembers old code, so delete what you do not need.
A block comment that begins with /** is a JSDoc comment. Editors such as VS Code read it to show hints, and TypeScript can use it to check plain JavaScript files.
Hover over add in your editor and you will see the description and parameter types. Common tags are @param, @returns, @throws and @example.
| Comment | Purpose |
|---|---|
#!/usr/bin/env node | Hashbang on the first line of a script so Unix systems run it with Node.js. It is valid syntax in modern JavaScript, not a normal comment. |
// @ts-check | Asks TypeScript-aware editors to type-check a JavaScript file. |
// eslint-disable-next-line | Tells ESLint to skip the next line. |
// TODO: and // FIXME: | Conventional markers that editors and tools can search for. |
//# sourceMappingURL=... | Generated by build tools to link minified code to its source. |
Do not remove the generated source map comment by hand in built files, because debuggers rely on it.
// Retry because the payment API sometimes times out is useful; // add 1 to i is noise.const isAdult = age >= 18; needs no explanation.<!-- --> is for HTML files; inside a <script> element you still write JavaScript comments.Engines skip comments while parsing, so the cost is negligible. Production builds usually strip them anyway during minification.
No. Inside quotes, // is just text: "https://example.com" is a normal string. Comments only exist in code.
In most editors, including VS Code, press Ctrl+/ (Cmd+/ on macOS) to toggle a line comment on the selected lines.
No. Standard JSON does not allow comments. Configuration files such as tsconfig.json or .jsonc files accept them only because their tools support an extended format.
Use // for short notes and /* ... */ for longer blocks. Use /** ... */ JSDoc comments to describe functions so editors can help other developers. Write comments that explain intent, keep them current, never nest block comments, and delete dead code instead of leaving it commented out.