Skip to main content

How to Write Comments in JavaScript

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
// <comment text>

/* <comment text> */
PartWhat 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:

main.js with a single-line comment, a price of 40 followed by a comment on the same line, a block comment over several lines, a quantity of 3, a console.log of price times quantity, a console.log line commented out, and a console.log of Done with a block comment inside the brackets.

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​

Terminal
node main.js
Output
120
Done

A zsh terminal where node main.js prints 120 and 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. price is 40 although a comment follows it on the same line, so price * quantity printed 120.
  • The commented-out console.log never 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, leaving console.log('Done').

Common Mistakes​

MistakeWhat happensDo 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 closedSyntaxError: Invalid or unexpected token, and nothing runsClose it with */
Expecting // inside a string to start a commentIt doesn't: console.log('https://example.com') printed the whole addressNothing to fix; text in quotes is never a comment
Commenting out only the first line of a statement that runs over two linesThe second line is still code: // console.log('a', above 'b'); gave SyntaxError: Unexpected token ')'Comment out every line of the statement

Troubleshooting​

SymptomLikely causeFix
SyntaxError: Unexpected identifier right after a commentA nested block comment ended earlyLook 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 initializationMake 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​