Debugging Techniques
Debugging is the process of finding out why code does not do what you expect and fixing the cause. In JavaScript, most debugging happens in the browser's developer tools, using the console, breakpoints and the network panel. Good debugging is less about guessing and more about narrowing down where the program's real behaviour first differs from what you intended.
Start by reading the error
When something breaks, open the Console panel first. A red message usually gives three useful facts: the error type, a description and a file name with a line number. TypeError: Cannot read properties of undefined (reading 'name') tells you that something before .name was undefined. Click the file link to jump to the exact line. The stack trace underneath lists the chain of function calls that led there, which often points to where the bad value came from.
Getting more from the console
console.log is the quickest tool, but its relatives are often more helpful:
const user = { id: 7, name: 'Asha', roles: ['admin', 'editor'] };
console.log({ user }); // label shows which variable this is
console.table(user.roles); // arrays and objects as a table
console.warn('Cart is empty'); // yellow, easy to spot
console.error('Save failed'); // red, includes a stack trace
console.group('Checkout');
console.log('step 1: validate');
console.log('step 2: charge');
console.groupEnd();
console.time('sort');
bigList.sort((a, b) => a.price - b.price);
console.timeEnd('sort'); // prints how long the sort took
console.assert(user.id > 0, 'User id should be positive');
Wrapping a variable in braces, as in console.log({ user }), prints its name alongside its value, which avoids confusion when you log many things.
Pausing code with breakpoints
Logging shows values you thought to print. Breakpoints show everything. In the Sources panel, click a line number to set a breakpoint, then trigger the code. Execution stops on that line and you can:
- hover over variables or read the Scope panel to see their current values,
- step over the next line, step into a function call, or step out of the current function,
- watch an expression that updates as you step,
- look at the Call Stack to see how you got there.
Writing the statement debugger; in your code does the same when developer tools are open. Conditional breakpoints, set by right-clicking a line number, pause only when an expression is true, such as item.id === 42 inside a long loop.
Narrowing down the problem
When the cause is not obvious, shrink the search area:
- Reproduce it reliably. Write down the exact steps that trigger the bug.
- Check your assumptions. Log or inspect the input to the failing function. Is it the type and shape you expected?
- Split in half. Check the value at the midpoint of the logic. If it is already wrong, the bug is earlier; if not, it is later.
- Reduce the code. Comment out unrelated parts or copy the failing piece into a small test file until the bug disappears, then add back the last thing removed.
For problems with requests, the Network panel shows each request's URL, status code, headers and response body. Many "my data does not show" bugs turn out to be a 404 or a response shaped differently from what the code reads.
A classic misleading log
In some browsers, logging an object shows a live reference. If you log an object and change it later, expanding it in the console may show the new values, not the ones at the moment of logging. When the timing matters, log a copy, for example console.log(structuredClone(state)) or JSON.stringify(state), or use a breakpoint instead.
Also clean up afterwards. Forgotten console.log calls and debugger statements in shipped code clutter the console and can expose data.
Core habits
Read the full error and stack trace, use labelled logs and the wider console API, pause with breakpoints when you need to see state, and narrow the search by halves. Check the Network panel for data problems, and remove debug code before you finish.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.