Sikau

JavaScript · Day 28 · about 25 min

Debugging

Finding and fixing mistakes on purpose.

Every programmer debugs

Professional developers spend a big part of every day finding bugs. It isn't failure — it's detective work. Today you'll learn a calm, step-by-step way of debugging.

The detective's method

  • Read the error. Which line? What kind of error?
  • Say what you expected and what actually happened.
  • Look inside with console.log — check what values really are.
  • Change one thing at a time, then run again.
function average(a, b) {
  const total = a + b;
  console.log("total is", total);
  return total / 2;
}

console.log(average("4", 6));

console.log("label", value) with a label makes your debugging logs easy to tell apart.

The rest of this lesson

  • Common bugsexplain
  • Bugs without error messagesexplain
  • Spot the bugpredict
  • Hunting a silent bugorder
  • Your turnwrite code
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 68 lessons of JavaScript