Browser DevTools
Debugger
Learn to find and fix bugs like a professional developer using breakpoints, step controls, and watch expressions in your browser's built-in debugger.
What Is a Debugger?
A debugger is a tool that lets you pause your JavaScript code while it runs, inspect what is happening at each step, and find exactly where things go wrong. Every modern browser (Chrome, Firefox, Edge, Safari) includes a powerful debugger inside its Developer Tools (DevTools).
Why Not Just console.log?
Many beginners add console.log() statements everywhere to see what their variables hold. While that works for simple cases, it quickly becomes messy and slow for real projects. The debugger gives you all that information and more, without modifying your code.
console.logOpening DevTools
Every major browser lets you open DevTools with a keyboard shortcut. Once open, navigate to the Sources tab (Chrome/Edge) or Debugger tab (Firefox) to access the debugger panel.
The Three Core Concepts
The debugger revolves around three fundamental ideas. Master these, and you can diagnose virtually any JavaScript bug.
Setting Breakpoints
Click the line number in the Sources panel to toggle a breakpoint. A red dot appears, marking that line. When the browser reaches that line during execution, it freezes and hands control to you.
2 let sum = 0;
3 for (let i = 0; i < items.length; i++) { 4 sum += items[i].price;
5 }
6 return sum;
7 }
Step Controls Explained
Watch and Scope Panels
While paused at a breakpoint, two panels on the right side of DevTools show you everything about the current state of your program.
sum > 100 or items.length), and the debugger evaluates it live at each pause.Step Over vs Step Into
This is the most important distinction for beginners. Both advance execution by one line, but they differ when that line calls another function.
| Step Over (F10) | Step Into (F11) | |
|---|---|---|
| Function call on line | Runs the entire function, lands on the next line | Jumps inside the function to its first line |
| When to use | You trust the called function is correct | You suspect the bug is inside that function |
| Speed | Faster: skips internal details | Slower: shows every internal step |
| Analogy | Driving past a building | Walking inside to explore each room |
The Debugging Workflow
Example 1: Off-by-One Bug
This function should return the last item of an array, but it returns "undefined". Let's use the debugger.
2 return arr[arr.length]; 3 }
4 getLast([10, 20, 30]); // undefined!
Example 2: Loop Bug with Stepping
This code should double every number, but the output is wrong. We set a breakpoint inside the loop and step through each iteration.
2 for (let i = 0; i <= nums.length; i++) {
▶3 nums[i] = nums[i] * 2; 4 }
Example 3: Step Into a Function
The main function calls a helper. The result is wrong, and we need to figure out which function has the bug.
2 return price - price * pct; // expects decimal
3 }
4
5 let final = applyDiscount(100, 20); 6 // Expected: 80, Got: -1900
Types of Breakpoints
Beyond clicking a line number, DevTools offers several specialized breakpoint types for different debugging scenarios.
i = 50. Only pauses when the condition is true.debugger Statementdebugger; in your code. Acts like a breakpoint, but lives in the source file itself.The Call Stack
When your code pauses, the Call Stack panel shows the chain of function calls that led to the current line, from the most recent (top) to the entry point (bottom).
Common Mistakes
Question 1 of 5
What happens when the browser reaches a line that has a breakpoint set on it?
Question 2 of 5
You are paused on a line that calls calculateTax(price). You suspect the bug is inside that function. Which step control should you use?
Question 3 of 5
Which panel shows you the current value of all variables in the function where execution is paused?
Question 4 of 5
You accidentally stepped into a long utility function. You want to finish it quickly and return to the code that called it. What do you press?
Question 5 of 5
What is the primary advantage of using the debugger over adding console.log() statements?
console.log, you do not need to guess which variables to print, edit your code, and re-run it.