Comment (JavaScript)

This page covers Comment in JavaScript. For a language-agnostic introduction, see Comment.

A comment in JavaScript is text the interpreter ignores. JavaScript has two comment syntaxes: single-line and block.

Single-line comment#

A single-line comment starts with // and continues to the end of the line.

// Everything on this line is a comment.
console.log("Hello, World!");

You can also write it inline, to the right of code:

console.log("Hello, World!"); // Everything to the right is a comment.

Block comment#

A block comment starts with /* and ends with */. Everything between those markers is ignored, regardless of how many lines it spans.

/*
This is a block comment.
It can span multiple lines.
*/
console.log("Hello, World!");

Block comments also work on a single line or inline:

/* Block comment on a single line */
console.log("Hello, World!"); /* Block comment inline */

Always close a block comment with */. If you forget, everything after the opening /* — including real code — will be treated as part of the comment and ignored.

Common Mistakes#

Using # instead of // # is the comment syntax in Python. In JavaScript, single-line comments always start with //.

Nesting block comments JavaScript does not support nested block comments. Writing /* outer /* inner */ */ will cause a syntax error — the first */ closes the comment, and the remaining */ is unexpected.

Leaving block comments unclosed An unclosed /* silently comments out everything that follows it in the file. This can be hard to diagnose because the code looks correct but is being ignored entirely.

Resources#