Comments in JavaScript

Imagine your mother gives you her special biryani recipe. Next to some steps, she has written small notes: "Don't add too much salt," or "Cook on low flame for 10 minutes."

These notes are not part of the recipe itself, but they help you understand it better.

In JavaScript, comments are exactly like these notes. They are lines that explain your code, and JavaScript completely ignores them when the program runs.

What Is a Comment?

A comment is a note written inside the code for humans to read. JavaScript skips it and does not run it.

Output:

Hello!

Only Hello! is printed. The comments are invisible to JavaScript.

Why Do We Need Comments?

  • To explain the code: Tell others (and your future self) why something is written.
  • To make code easy to read: Long programs become easier to follow.
  • To help teamwork: Other developers can quickly understand your logic.
  • To test and debug: You can temporarily turn off a line of code without deleting it.

Code you write today may look confusing to you after 3 months. Comments save you from that confusion!


Types of Comments in JavaScript

JavaScript has two main types of comments.

1. Single-Line Comments //

Start with two forward slashes //. Everything after // on that line becomes a comment.

Key points:

  • The comment works only until the end of that line.
  • It can be written on its own line or at the end of a code line.
  • It is the most commonly used type of comment.

2. Multi-Line Comments /* */

Start with /* and end with */. Everything between them is a comment, even if it runs across many lines.

Key points:

  • Useful for longer explanations.
  • Don't forget to close it with */, otherwise the rest of your code will also become a comment!
  • You cannot put one /* */ comment inside another:

3. Documentation Comments /** */ (Bonus)

This is a special form of the multi-line comment. It starts with /** and is used to describe functions. This style is called JSDoc.

Key points:

  • @param describes the inputs, and @returns describes the output.
  • Code editors like VS Code show these notes as hints when you use the function.
  • It is very useful in big projects and team work.

Using Comments to Disable Code

Sometimes you want to stop a line from running for testing, without deleting it. Just put // before it. This is called commenting out code.

Output:

Step 1: Order placed
Step 3: Order confirmed

Shortcut: In VS Code, select the lines and press Ctrl + / (Windows) or Cmd + / (Mac) to comment or uncomment them quickly.


Good Comments vs Bad Comments

Comments are helpful only when they are written well.

❌ Bad: repeating what the code already says

let age = 25;   // set age to 25
age++;          // increase age by 1

These comments add nothing. The code already says the same thing.

✅ Good: explaining why

// Minimum age required to apply for a driving licence in India
const MIN_AGE = 18;

// Add 5 bonus marks for students who submitted the project early
marks += 5;

These comments explain the reason, which the code alone cannot tell you.

Tips for writing good comments

  • Explain why, not what.
  • Keep comments short and clear.
  • Update comments when you change the code. Old, wrong comments are worse than no comments!
  • Don't comment every line. Clear variable names often explain the code themselves:
// ❌ Needs a comment
let x = 500;   // delivery charge

// ✅ Explains itself
let deliveryCharge = 500;

Quick Summary Table

TypeSyntaxUsed for
Single-line// commentShort notes, one line
Multi-line/* comment */Longer explanations
Documentation/** comment */Describing functions (JSDoc)

Key Takeaways

  • Comments are notes for humans, and JavaScript ignores them completely.
  • Use // for single-line comments and /* */ for multi-line comments.
  • Use /** */ (JSDoc) to describe functions and get helpful hints in your editor.
  • You can comment out code to turn it off temporarily while testing.
  • Good comments explain why, not what, and they must be kept up to date.