01 / 21

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.

JavaScript

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.log
You must guess where to add logs, re-run the code, and then remove them later.
Debugger
You pause on any line, inspect all variables instantly, and never touch your source code.

Opening 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.

1
Windows / Linux: Press F12 or Ctrl + Shift + I
2
Mac: Press Cmd + Option + I
3
Navigate to the Sources panel and find your JavaScript file in the left sidebar.

The Three Core Concepts

The debugger revolves around three fundamental ideas. Master these, and you can diagnose virtually any JavaScript bug.

Breakpoints
Markers you set on specific lines to tell the browser: "Pause here."
Step Controls
Buttons that advance execution one line (or one function call) at a time.
Watch / Scope
Panels showing the current value of any variable or expression you choose.

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.

1 function calculateTotal(items) {
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

Resume F8 / Cmd+\ Step Over F10 Step Into F11 Step Out Shift+F11 Run until the next breakpoint Execute current line, skip over function details Jump inside a called function to see its code Finish current function, return to the caller FAST FORWARD NEXT LINE GO DEEPER GO UP

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.

Watch Panel
You type in any expression (like sum > 100 or items.length), and the debugger evaluates it live at each pause.
Scope Panel
Automatically shows all variables in the current scope: Local (function variables), Closure, and Global.

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

1
Reproduce the bug. Trigger the exact action that causes the error or wrong result.
2
Set a breakpoint on the line you suspect, or just before the output goes wrong.
3
Trigger the action again. The browser pauses at your breakpoint.
4
Inspect variables in the Scope panel. Are they what you expected?
5
Step through line by line. Watch the values change. Find the line where reality diverges from your expectation.
6
Fix the code and remove or disable your breakpoints.

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.

1 function getLast(arr) {
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.

1 let nums = [1, 2, 3];
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.

1 function applyDiscount(price, pct) {
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.

Line Breakpoint
Click a line number. Pauses every time that line executes.
Conditional
Right-click a line number, add a condition like i = 50. Only pauses when the condition is true.
debugger Statement
Write debugger; in your code. Acts like a breakpoint, but lives in the source file itself.
DOM Breakpoint
Pauses when a specific HTML element is modified, removed, or gets a new child node.

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).

applyDiscount line 2 (paused here) processOrder line 15 handleClick line 8 (anonymous) entry point TOP BOTTOM Most recent First called

Common Mistakes

✕ Forgetting to Remove Breakpoints
Leaving breakpoints active after debugging causes the page to freeze unexpectedly next time the code runs.
✕ Using Resume Instead of Step
Pressing Resume (F8) when you meant Step Over (F10) skips past the bug entirely. Know your shortcuts.
✕ Ignoring the Call Stack
The bug might not be in the function you're looking at. Always check the Call Stack to see what called this function and with what arguments.

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?