- -- operator
- -= operator
- ++ operator
- += operator
- Accessing and setting content
- Array concat() method
- Array indexOf()
- Array length
- Array pop()
- Array shift
- Arrays
- Booleans
- Braces
- Callback function
- Calling the function
- Class
- Closure
- Code block
- Comment
- Conditions
- Console
- Constructor
- Creating a p element
- Data types
- Date getTime()
- Destructuring
- Else
- Else if
- Enum
- Equals operator
- Error Handling
- ES6
- Event loop
- Events
- Extend
- Fetch API
- Filter
- For loop
- forEach()
- Function
- Function bind()
- Function name
- Greater than
- Head element
- Hoisting
- If statement
- includes()
- Infinity property
- Iterator
- JSON
- Less than
- Local storage
- Map
- Methods
- Module
- Numbers
- Object.keys()
- Overriding methods
- Parameters
- Promises
- Random
- Reduce
- Regular expressions
- Removing an element
- Replace
- Scope
- Session storage
- Sort
- Splice
- String
- String concat()
- String indexOf()
- Substring
- Switch statement
- Template literals
- Ternary operator
- Tile
- Type conversion
- While loop
JAVASCRIPT
JavaScript Comment: Syntax, Usage, and Examples
A JavaScript comment lets you write notes, explanations, or disable code without affecting how the script runs. You can use comments to explain your logic, describe functions, or prevent certain lines from executing. Whether you're working solo or collaborating with a team, understanding how to comment JavaScript properly makes your code cleaner and easier to maintain.
How to Use JavaScript Comments
JavaScript supports two types of comments:
- Single-line comments using
//
- Multi-line (block) comments using
/* */
Single-line Comment
Use //
to comment out a single line:
// This is a single-line comment
let name = "Alice"; // You can also comment at the end of a line
Multi-line (Block) Comment
Use /* */
to comment out multiple lines or blocks of code:
/*
This is a block comment.
You can write across multiple lines.
*/
let age = 30;
You can also use a comment block in JavaScript to temporarily disable multiple lines of code during debugging or testing.
When to Use Comments in JavaScript
Describe What the Code Does
// Calculate the area of a rectangle
let width = 5;
let height = 10;
let area = width * height;
A simple line of context makes code easier to understand, especially when revisiting it later.
Disable a Line of Code Temporarily
// console.log("This line won't run");
Useful when debugging, especially to narrow down issues without deleting code.
Create Section Headers
Use comments to visually separate your code into parts:
// --- Event Handlers ---
function onClick() {
// ...
}
This helps organize large files and makes navigation easier.
Document Complex Logic or Decisions
/*
Used Math.floor instead of rounding
to ensure we never exceed the array index.
*/
let index = Math.floor(Math.random() * array.length);
Good comments explain why, not just what. If the logic isn’t obvious, write a note.
Examples of JavaScript Comment in Practice
Inline Notes
let temp = 100; // Fahrenheit
let celsius = (temp - 32) * 5 / 9;
Quick hints like this clarify variable meaning and reduce guesswork.
Disable Functionality Temporarily
// showAlert(); // Temporarily disabled for testing
When testing features or isolating bugs, it’s faster to comment out code than delete and rewrite it.
Add a TODO List
// TODO: Add input validation
// FIXME: Handle edge case for negative values
These tags help track improvements or bugs during development. Many editors highlight them automatically.
Learn More About Commenting in JavaScript
Nesting Comments (What to Avoid)
JavaScript does not support nesting comment blocks:
/*
This is fine.
// This is okay too.
But you can't do:
/* nested block */
*/
Nested block comments will break the code. Stick to single-line comments inside blocks if needed.
Use Comments to Explain “Why”, Not Just “What”
Bad:
// Multiply width by height
let area = width * height;
Better:
// Calculate area to determine how much paint is needed
let area = width * height;
Explain the purpose, not just the operation.
JavaScript Comment Block for Documentation
You can also use structured comments (known as JSDoc) for documenting functions, parameters, and return types:
/**
* Calculates the sum of two numbers.
* @param {number} a - The first number.
* @param {number} b - The second number.
* @returns {number} Sum of a and b.
*/
function add(a, b) {
return a + b;
}
Many code editors and documentation generators can use this format to auto-generate helpful tooltips and docs.
Styling Comments for Readability
Use consistent formatting to make comments stand out and keep them useful:
// ------------------
// Section: Setup
// ------------------
This creates visual anchors in your script, especially helpful in large codebases.
How to Comment in JavaScript Efficiently
- Use comments sparingly. If the code is self-explanatory, don’t over-comment.
- Keep comments up to date. Outdated comments cause confusion.
- Don’t write obvious comments like:
// Set count to 0
let count = 0;
If it’s clear, you don’t need to say it.
Use Comments for Debugging
If you’re troubleshooting, comment out chunks of code to isolate the issue:
/*
fetchData();
processData();
renderData();
*/
// Only calling fetch for now:
fetchData();
Later, you can restore the original block easily.
Add Explanations for Workarounds or Hacks
If you’re doing something strange for a good reason, say so:
// Using setTimeout with 0 delay to defer execution
setTimeout(() => initialize(), 0);
Prevent future devs (or your future self) from rewriting something they don’t understand.
Using JavaScript comment methods correctly helps keep your code readable, maintainable, and collaborative. Whether you're annotating logic, disabling blocks temporarily, or documenting functions for your team, comments provide vital context that pure code can't.
Writing clear, purposeful comments improves communication within your projects and saves time when debugging or onboarding others. Avoid cluttering your files with excessive notes—but when used thoughtfully, comments in JavaScript are an essential part of professional coding practice.
Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.