JS Debugging
JavaScript Debugging: Finding and Fixing Bugs Methodically
JavaScript debugging is the process of inspecting runtime values, tracing code execution, and figuring out why it produces unexpected results. Most beginners start and stop with console.log(). Learning a few more Console methods, breakpoints, and the browser's DevTools will usually get you to the cause of a bug faster. This guide covers the tools worth knowing, a working example, and the habits that prevent common debugging mistakes.
A Simple Debugging Routine
Before touching any tool, a repeatable routine saves time:
Reproduce the bug. Find the smallest set of steps or input that triggers it every time.
Read the error message and stack trace. They often point straight to the file and line.
Form a guess. Decide which value or branch is probably wrong.
Inspect, don't assume. Use a log or breakpoint to check that value.
Fix one thing, then retest. Changing several things at once hides which change worked.
Useful Console Methods Beyond console.log()
The Console API offers several methods that make noisy output easier to read:
console.log({ user, total }): Wrapping variables in curly braces prints labeled key-value pairs, so you always know which value belongs to which variable.
console.table(arrayOfObjects): Shows an array of objects as a table in browser DevTools and Node.js.
console.warn() and console.error(): Highlight messages in yellow or red. In browsers, they also include a collapsible stack trace.
console.time("label") and console.timeEnd("label"): Print the elapsed time between two lines. The label must match in both calls.
Breakpoints and the debugger Statement
A breakpoint pauses your code at a chosen line so you can look around. You can set one by clicking a line number in the Sources panel of DevTools, or by writing debugger; in your code. The debugger; statement only pauses when DevTools is open.
While paused, you can hover over variables to see their values, read the Call Stack to see how you got there, and step through code line by line. The Chrome DevTools JavaScript debugging guide walks through each control.
A Practical Debugging Example
The function below skips invalid items and times its own calculation:
function computeCartTotal(items) {
console.time("cartCalc");
let subtotal = 0;
for (const item of items) {
if (typeof item.price !== "number" || item.price < 0) {
console.warn("Skipping invalid price item:", item);
continue;
}
subtotal += item.price * item.qty;
}
console.timeEnd("cartCalc");
return Number(subtotal.toFixed(2));
}
const sample = [
{ name: "Notebook", price: 8.5, qty: 2 },
{ name: "Broken Item", price: -5, qty: 1 },
];
console.table(sample);
console.log("Final subtotal:", computeCartTotal(sample));What You Should See
Run it in the browser console or in Node.js. If it works, you will see:
A table with two rows from console. table(sample).
A warning that "Broken Item" was skipped because its price is negative.
A timing line such as cartCalc: 0.1ms. The exact number will differ on every run.
Final subtotal: 17, because only the notebook counts (8.5 × 2).
To pause inside the loop, add a debugger; right before subtotal += ..., reload the page with DevTools open, and watch item and subtotal change on each pass.
Conditional Breakpoints and Exception Pausing
Two DevTools features save a lot of time in loops and unfamiliar code:
Conditional breakpoint. In the Sources panel, right-click a line number and choose to add a conditional breakpoint. Enter an expression like item. price < 0. Execution pauses only when that expression is true, so you don't step through hundreds of iterations.
Pause on exceptions. Enable the pause-on-exceptions option in the Sources panel debugger controls. DevTools then freezes on the line where an error is thrown, instead of leaving you with only a console message. You can choose to pause on all exceptions or only uncaught ones.
Common JavaScript Debugging Mistakes
Committing
debugger;statements to production: Leavingdebugger;in shipped files pauses execution for users who open DevTools. Configure ESLintno-debuggerto block it.Debugging without reading the full stack trace: Always inspect the first file and line number in your own codebase listed in the error stack trace before guessing.
Logging mutable objects and reading expanded views later: DevTools evaluates expanded object previews lazily; log
structuredClone(obj)orJSON.stringify(obj)to snapshot exact state at that moment.
For related topics, see our lessons on JavaScript errors and JavaScript try...catch.
Conclusion
Good JavaScript debugging is mostly a method. Reproduce the bug, read the error, inspect real values, and change one thing at a time. Use console.table() and labeled logs for quick checks, breakpoints for deeper investigation, and source maps for production issues. For your next step, read about handling errors with try...catch.
Frequently Asked Questions
What is a conditional breakpoint in DevTools?
Right-click a line number in the Sources panel and enter a condition like user.id === 42. Execution pauses only on the iteration where that expression is truthy.
How do I catch unhandled errors before they vanish?
Enable "Pause on caught/uncaught exceptions" in the DevTools Sources panel to freeze execution at the exact line an error is thrown.
What are source maps used for?
Source maps (.map files) link minified production bundles back to your original readable ES6 or TypeScript lines inside DevTools.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.