JavaScript Comments
Subject: JavaScript
JavaScript comments are used to write notes, explanations, or temporarily disable code without affecting the program’s execution. Comments improve code readability, help with debugging, and assist teams in understanding code logic.
Types of Comments in JavaScript
JavaScript supports two types of comments:
- Single-line Comments
- Multi-line Comments
1. Single-line Comments
Start with //. Everything after // on that line is ignored by JavaScript.
Syntax
Example
2. Multi-line Comments
Wrap your comment with /* */. This type of comment can span across multiple lines.
Syntax
Example
When to Use Comments
- To explain complex logic
- To label sections of code
- To debug code by disabling certain lines
- To leave reminders or TODO notes for developers
Best Practices for Commenting
- Keep comments short, relevant, and updated
- Don’t state the obvious (avoid redundant comments)
- Use comments to explain “why”, not just “what”
- Use consistent formatting (indentation, spacing)