Developer tools
debugger and breakpoints
A freeze-frame of the program: stop at the right line and look at every variable without adding a single console.log.
Why it exists
console.log shows only what you thought of printing — and you have to rerun the program every time. A debugger stops time: while paused you see everything — parameters, local variables, closures, this, the whole call stack. You can step one line forward and see what changed, step into a call, or evaluate any expression in the console right in the current context. When you don't know what to look for, that's faster than a dozen logs.
Where it's used
- A tricky condition or loop: stop at the right iteration and see why the if took the wrong branch.
- Someone else's code and libraries: step into a call and see what the function actually does with your data.
- Event handlers and requests: DevTools breakpoints on a click, on a DOM element change, on a request to a URL (XHR/fetch breakpoints).
- Node.js and tests:
node --inspectand the VS Code debugger stop at the samedebuggerand at breakpoints set in the editor.
Syntax
debugger; // pause here if DevTools are open
if (item.price < 0) debugger; // pause only in a suspicious case
// DevTools → Sources: click a line number = a breakpoint
// Buttons: Resume (F8), Step over (F10), Step into (F11), Step out (Shift+F11)Examples
function applyDiscount(price, percent) {
debugger; // with DevTools open, the browser stops here
return price - (price * percent) / 100;
}
applyDiscount(1000, 15); // → 850 — without DevTools, debugger does nothingconst cart = [
{ name: "Cat food", price: 300 },
{ name: "Toy mouse", price: -120 },
{ name: "Cat bed", price: 900 },
];
let total = 0;
for (const item of cart) {
if (item.price < 0) debugger; // pause only on the suspicious item
total += item.price;
}
total; // → 1080 — should be 1320: the toy mouse is to blamefunction average(numbers) {
let sum = 0;
for (let i = 0; i <= numbers.length; i++) {
// a breakpoint here shows: on the last step i === 3, numbers[i] === undefined
sum += numbers[i];
}
return sum / numbers.length;
}
average([4, 5, 6]); // → NaN — stepping reveals the extra iterationi the sum turns into NaNResults after // → aren't just written down — an automatic check runs and verifies them before every site update.
Why it works this way
debugger is an ordinary statement of the language. If a debugger is attached to the program (DevTools are open, node --inspect is running, or the editor is debugging), the engine hits it and freezes execution: the call stack stays as it is, and no more code runs. With no debugger attached, the statement is simply skipped.
A breakpoint set by clicking a line number in the Sources tab works the same way but doesn't require changing the code. There are conditional breakpoints ("stop only if id === 42") and logpoints — they print a value without stopping, like a console.log without editing the file.
While paused, the right side shows the Scope panel (every variable: local, closure, global), Call Stack (who called whom — the same stack as in an error, but live: you can click through it) and Watch (expressions you're tracking). The stepping buttons: Step over runs the whole line, Step into goes inside a call, Step out finishes the current function and leaves it.
In the Tasky sandbox code runs isolated and with a time limit, so we describe the debugger in words; try it on your own computer: open DevTools (F12) and run code with debugger in the Console tab.
Traps
How it differs from…
consolemethods for debuggingconsole.logdoesn't stop the program and shows only what you chose; it's handy to leave in a loop for a while and see the history of values. A debugger shows everything, but at a single point in time.
When to pick something else
It doesn't suit time-dependent code: while the program is paused, timers, animations and server responses carry on with their lives, and the bug may disappear (or a new one may appear). Logs with timestamps work better there. And never leave debugger in code that ships to users.