How to use comments and read errors

Write useful comments

Use // for one line and /* ... */ for a block. Explain why code exists rather than translating every statement.

// Prices are stored in cents to avoid rounding surprises.
const priceInCents = 1299;

/* Convert once at the display boundary. */
console.log(priceInCents / 100);

Read an error message

Start with the error type and message, then follow the stack to the first line in your own file. The reported line is where JavaScript noticed the problem.

const total = 12;
console.log(totl);
// ReferenceError: totl is not defined

Recognize common errors

  • SyntaxError: the source cannot be parsed; inspect nearby punctuation
  • ReferenceError: a name is missing, misspelled, or unavailable
  • TypeError: a value does not support the attempted operation
  • A logic error runs without throwing but produces the wrong result

Debug with evidence

Log values and types near the failure, set a breakpoint in browser developer tools, and reduce the program to the smallest failing example.

function double(value) {
  console.log({ value, type: typeof value });
  return value * 2;
}

console.log(double("6")); // Works by coercion, but may hide a bug.