Skip to content
blog
2 min read

Comments

Understand JavaScript comments with // for single-line and /* */ for multi-line notes. Comments help document and explain your code.

Comments are notes in your code that JavaScript ignores completely. They exist for humans — your future self, your teammates, anyone reading the code later.

Single-Line Comments

Use // for a note on one line:

// Calculate total price with tax
const total = price * 1.11;
const width = 800; // pixels

Multi-Line Comments

Use /* */ when you need more space:

/*
This function handles authentication.
It checks the token, validates expiry,
and returns the user object or null.
*/
function authenticate(token) {
// ...
}

What to Comment

Good comments explain why, not what. The code already shows what it does:

// Bad — restates the obvious
// Increment i by 1
i++;
// Good — explains a non-obvious decision
// Skip index 0 — first element is always the header row
for (let i = 1; i < rows.length; i++) { ... }

If you feel the need to comment what the code does, consider whether the code could be clearer without the comment — better naming, extracted functions, simpler logic.

Temporarily Disabling Code

Comments are useful for debugging — commenting out code to test if something causes a problem:

function process(data) {
validate(data);
// transform(data); // disabled until bug is fixed
save(data);
}

Remove these before committing. “Commented-out code” in version control is noise — if you need it later, git history has it.