When writing JavaScript code, it’s important to make your code easy to read and understand. This is where comments come in. Comments are notes written inside the code that JavaScript ignores during execution. They are mainly used to explain what the code does.


What Are Comments in JavaScript?

Comments are used to describe code, give instructions, or temporarily disable parts of code during testing. They help other developers (and your future self) understand the logic better.

JavaScript supports two types of comments:

  1. Single-line comments

  2. Multi-line comments


Single-Line Comments

Single-line comments start with //. Everything written after // on that line is ignored by JavaScript.

Example:

You can also place a comment after a statement:


Multi-Line Comments

Multi-line comments start with /* and end with */. They are useful when you need to write longer explanations.

Example:


Why Use Comments?

  • To explain complex logic

  • To make code more readable

  • To help others understand your code

  • To temporarily disable code during debugging

Example of disabling code:


Good Practices for Writing Comments

  • Keep comments short and clear

  • Avoid explaining obvious code

  • Update comments when code changes

  • Do not overuse comments


Conclusion

Comments are a small but powerful part of JavaScript. They help you write clean, readable, and maintainable code. Using comments properly is a good habit, especially when you are learning or working in a team.