Comments
Understand JavaScript comments with // for single-line and /* */ for multi-line notes. Comments help document and explain your code.
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.
Use // for a note on one line:
// Calculate total price with taxconst total = price * 1.11;
const width = 800; // pixelsUse /* */ 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) { // ...}Good comments explain why, not what. The code already shows what it does:
// Bad — restates the obvious// Increment i by 1i++;
// Good — explains a non-obvious decision// Skip index 0 — first element is always the header rowfor (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.
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.