How to Write Comments in JavaScript
A comment is a note in your code that JavaScript skips. // starts a comment that runs to the end of the line, and /* ... */ wraps a comment that can span several lines or sit in the middle of one. Put // in front of a line of code and that line no longer runs.
Syntax
// <comment text>
/* <comment text> */
| Part | What it does |
|---|---|
// <comment text> | A single-line comment. Everything from // to the end of the line is skipped, so it can follow code on the same line. |
/* <comment text> */ | A block comment. Everything between /* and the next */ is skipped, across as many lines as you like. |
MDN's Lexical grammar page describes both kinds.
The Example
The example multiplies a price by a quantity and prints the result, with comments around the code: a // comment on its own line, one after code, a block comment over several lines, a line of code commented out, and a block comment in the middle of a line.
Prerequisites
- Node.js. This guide uses Node.js 26.13.1.
Step 1: Write the Code
Create a file named main.js:

// A single-line comment: Node.js skips everything after the two slashes.
const price = 40; // A comment can also follow code on the same line.
/*
A block comment starts with slash-star
and can span several lines.
*/
const quantity = 3;
console.log(price * quantity);
// console.log('This line is commented out, so it never runs.');
console.log(/* even in the middle of a line */ 'Done');
Step 2: Run It
node main.js
120
Done

The command is the same in zsh, bash and PowerShell.
What Happened
- None of the comment text appeared, and none of it caused an error: Node.js skipped every comment.
- The code next to the comments still ran.
priceis40although a comment follows it on the same line, soprice * quantityprinted120. - The commented-out
console.lognever ran. That's the quick way to switch a line off while you test something. /* even in the middle of a line */disappeared from the last line, leavingconsole.log('Done').
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Putting one block comment inside another, /* outer /* inner */ still outer? */ | The first */ ends the comment, so still outer? */ is read as code: SyntaxError: Unexpected identifier 'outer' | Use // on each line of the outer comment, or remove the inner */ |
Leaving a block comment open, /* never closed | SyntaxError: Invalid or unexpected token, and nothing runs | Close it with */ |
Expecting // inside a string to start a comment | It doesn't: console.log('https://example.com') printed the whole address | Nothing to fix; text in quotes is never a comment |
| Commenting out only the first line of a statement that runs over two lines | The second line is still code: // console.log('a', above 'b'); gave SyntaxError: Unexpected token ')' | Comment out every line of the statement |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
SyntaxError: Unexpected identifier right after a comment | A nested block comment ended early | Look for a second */ in the same comment |
SyntaxError: Invalid or unexpected token near a /* | A block comment without its closing */ | Add the */ |
An odd error on the line after a // | The // cut a statement in half, so what's left joins the next line: const total = price * // 3; made the next line part of it, and gave ReferenceError: Cannot access 'total' before initialization | Make sure the code before // is a complete statement |
FAQ
Is there a shortcut for commenting out lines?
In Visual Studio Code, select the lines and press Cmd+/ on macOS or Ctrl+/ on Windows and Linux. It adds // to each line, and pressing it again removes them.
What is a comment that starts with /**?
It's a block comment too, called a JSDoc comment. JavaScript skips it like any other, but editors read it to show descriptions of your functions and their parameters.
Do comments make my code slower?
No. JavaScript skips them when it reads the file, and tools that make code smaller for the web usually remove them.
Conclusion
Use // for notes on one line and for switching a line of code off, and /* ... */ for longer notes. Block comments can't be nested: the first */ always ends the comment.
Additional Resources
- Lexical grammar: Comments, on MDN.
- JSDoc, the documentation for JSDoc comments.
- Default keyboard shortcuts, in the Visual Studio Code documentation, which lists Toggle Line Comment.
