🎓 A1Learner
🔍

JavaScript Comments

A comment is a note inside code that the browser ignores. Comments explain code to the people who read it.

A note comment

The comment sits above the code and explains it. The code below the comment runs as normal.

// Print a greeting
console.log("Hello");

Code switched off

The first log line sits inside a block comment, so that line never runs. Only the live line below the comment prints.

/*
console.log("Hidden line");
*/
console.log("Live line");

A comment at the end

A line comment can follow code on the same line. The code before the comment still runs.

console.log("Shown"); // This note explains the line

Example

// Count report
console.log("Start");
/*
console.log("Old line");
*/
console.log("End"); // The report is done

Key points

  • Line comment: a note that starts with // and runs to the end of the line
  • Block comment: a note between /* and */ that can span several lines
  • The browser skips comments when it runs a script.
  • A comment can switch a line of code off without deleting it.

In this example, the first comment labels the program. The first log line prints Start. The block comment holds an old log line, and that line never runs. The last line prints End, and its end comment explains the line.

Try it Yourself

Edit the code, then press Run.

Runs in your browser — the preview under the editor is the real result. Nothing is sent to a server. The code runner panel (right) does the same with more room.

Exercise

What happens to a line of code inside a /* */ comment?