Debugging means finding and fixing mistakes (bugs) in your code. Bugs are normal: every programmer writes them every day. The skill is not avoiding them, it is locating them quickly. The key idea of this whole section: when code fails, beginners guess; debuggers check facts.
Code fails for two broad reasons. A syntax error breaks the grammar rules, so the script never runs (JS Errors). A logic error runs fine but does the wrong thing, often with no message at all (JS Silent Errors). Logic errors are the hard ones, because nothing tells you where to look.
Every browser has built-in developer tools with a console that shows errors and messages from JavaScript. If your code "does nothing," the console almost always says why. Open it with:
| Browser | How to open the console |
|---|---|
| Chrome / Edge (Windows) | F12, or Ctrl+Shift+J |
| Chrome / Edge (Mac) | Cmd+Option+J |
| Any browser | Right-click the page, choose Inspect, then the Console tab |
| Firefox | F12, or menu: More tools, Web Developer Tools |
| Safari | Settings, Advanced, turn on "Show features for web developers," then Develop, Show JavaScript Console |
console.log() prints values to the console, so you can see what your code is really doing instead of what you think it is doing:
let price = 50;
let quantity = 3;
let total = price * quantity;
console.log("Total:", total);
Many bugs happen because you assume a value is something it is not. Check the value and its type:
let x = 5;
let y = "5"; // maybe it came from a text box
console.log(x + y, typeof (x + y));
console.log(x + Number(y));
The log shows the bug right away: y was a string, so + joined instead of adding (JS Type Coercion).
| Message | Means | Check |
|---|---|---|
| ReferenceError: myValue is not defined | That name does not exist | Check spelling, and that it is declared with let or const |
| TypeError: Cannot read properties of undefined (reading 'length') | The value before .length is undefined | Log that value and find out why it is empty |
Click the file and line number on the right of an error in the console to jump straight to the line. Debug Errors covers many more messages.
Using = (assign) instead of === (compare) in a condition:
let x = 10;
if (x = 5) {
console.log("This runs");
}
console.log(x);
Fixed: if (x === 5). If an if behaves strangely, check it for a lone = first.
console.log()The next lessons go deeper: Debug Console (more than console.log), Debug Breakpoints (pausing code to look inside), Debug Errors (what each message means), and Debug Async (code that runs later).
A student expects 10 but sees 55. What is the most likely cause?
let a = 5;
let b = document.getElementById("box").value; // the user typed 5
console.log(a + b);
5 + "5" joins them into "55". Convert first: a + Number(b). (getElementById("box").value finds the element with id "box" and reads what was typed in it.)What is the first thing to do when your code "does nothing"?
What do the four steps Read, Reproduce, Reduce, Fix mean?
ReferenceError on purpose and read the line number in the errorif (x = 5) bug and fix it