Skip to content
Practical SkillsChapter 47 / 47Beginner5 min read

Debugging JavaScript

Learn how to debug JavaScript with DevTools, including console methods, breakpoints, the debugger keyword and the Network tab.

On this page

1. What is debugging?

The biggest JavaScript tool is the browser's DevTools. In Chrome, Edge and Firefox it opens with F12 or Cmd + Option + I (Mac). It has panels like Console, Sources, Network and Elements.

2. Why does it matter?

Every developer writes bugs. The difference is that a good developer finds them quickly. Changing code by guessing wastes time, while debugging tools take you straight to the cause.

Fixing by guessingProper debugging
Change the code and hopeReproduce the bug first
console.log everywhereSee the full state at a breakpoint
No idea where the value changedRun the code step by step
Cannot understand the API problemSee the request and response in the Network tab

3. Where is it used?

ProblemTool
A value is wrongConsole or a breakpoint
An error appearsThe error message and stack trace in the Console
API data is not comingThe Network tab, status code and response
An element looks wrongThe Elements panel, CSS and DOM
The page is slowThe Performance panel

4. How does it work?

Debugging follows a simple process that fits every bug:

Debugging process
Reproduce the bug
Read the error message and stack trace
Set a breakpoint on the suspect line
Check the variables and the call stack
Understand the cause and fix it
Test again

A stack trace tells you which file, which line and which chain of functions the error came from. Pay attention to the top line of the error message and the first line that belongs to your own file.

5. Code example

Console methods

js
const user = { name: "Ali", age: 22 };
const users = [user, { name: "Sara", age: 25 }];

console.log("user:", user);
console.table(users);
console.warn("This is only a warning");
console.error("Something went wrong");

console.time("loop");
for (let i = 0; i < 100000; i++) {}
console.timeEnd("loop");

The debugger keyword

js
function calculateTotal(items) {
  let total = 0;
  for (const item of items) {
    debugger;
    total += item.price * item.qty;
  }
  return total;
}

calculateTotal([{ price: 100, qty: 2 }, { price: 50 }]);

If DevTools is open, the code will pause on the debugger line. When you look at item there, you will see that the second item has no qty at all, which is why total becomes NaN.

How does the code work?

ItemMeaning
console.log("user:", user)A value with a label, easy to recognize
console.tableShows an array of objects as a table
console.warn / console.errorYellow and red lines, easy to filter
console.time / timeEndMeasures how long the code takes to run
debuggerPauses here if DevTools is open
Step over (F10)Go to the next line
Step into (F11)Go inside the function

Breakpoints without changing code

Open the file in the Sources panel and click on the line number. For a conditional breakpoint, right click and write a condition, like item.qty === undefined. This way the code pauses only when the problem happens.

Interview tip

The answer to "How do you find a bug?" should not be just console.log. Describe the process: reproduce, stack trace, breakpoints, the Network tab, and testing after the fix.

6. Common mistakes

Mistake: Changing code without reading the error message

Why? The error message often tells you directly what is undefined and on which line.

Better: Read the full error and stack trace first, then go to that line.

Mistake: Leaving console.log and debugger in production

Why? Users see useless data in their console, and sometimes sensitive information leaks too.

Better: Remove them before committing, or add the ESLint no-console and no-debugger rules.

Mistake: Logging an object and expanding it later

Why? The console holds an object reference, so expanding it shows the current value, not the value at the time of the log.

Better: Log structuredClone(obj) or JSON.stringify(obj) if you need the value at that moment, or use a breakpoint.

Mistake: Ignoring the Network tab

Why? Many bugs are not in the frontend, but in an API response like 401, 404 or 500.

Better: For a fetch bug, check the status code and response body in the Network tab first.

7. What to learn before and after

Final quick revision

PointDetail
DevToolsF12, Console, Sources, Network, Elements
Consolelog, table, warn, error, time
BreakpointClick a line or use the debugger keyword
Stack traceShows the path of the error
NetworkCheck the status code and response