8 minutes
The Browser Source Debugger
The browser's built-in source debugger is one of the most powerful tools available to web developers for troubleshooting JavaScript code. Whether you're dealing with unexpected behavior, runtime errors, or simply trying to understand how your code executes, the debugger provides invaluable insights into your application's inner workings.
Opening the Sources Debugger
Using Developer Tools
The easiest way to access the sources debugger is through your browser's Developer Tools:
- Chrome/Edge: Press
F12orCtrl+Shift+I(Windows/Linux) orCmd+Option+I(Mac) - Firefox: Press
F12orCtrl+Shift+I(Windows/Linux) orCmd+Option+I(Mac) - Safari: Press
Cmd+Option+I(Mac) - you may need to enable Developer Tools first
Once Developer Tools are open, click on the "Sources" tab (Chrome/Edge) or "Debugger" tab (Firefox).
[Image placeholder: Screenshot showing the Developer Tools with the Sources/Debugger tab highlighted]
Alternative Access Methods
You can also access the debugger by:
- Right-clicking on any webpage and selecting "Inspect" or "Inspect Element"
- Using the browser menu: More Tools → Developer Tools
- Adding
debugger;statements directly in your JavaScript code
Navigating the Sources Panel
The Sources debugger interface consists of several key areas:
File Navigator
The left panel shows all loaded scripts, stylesheets, and resources. You can expand folders to see the file structure and click on any JavaScript file to view its source code.
[Image placeholder: Screenshot of the file navigator panel showing expanded folder structure with JavaScript files]
Code Editor
The center panel displays the source code of the selected file. This is where you'll set breakpoints, view line numbers, and see your code execution pause.
[Image placeholder: Screenshot of the code editor panel with JavaScript code displayed and line numbers visible]
Debugging Panels
The right panel contains several sub-panels for inspecting variables, call stacks, and breakpoints:
- Scope: Shows local and global variables
- Call Stack: Displays the function call hierarchy
- Breakpoints: Lists all active breakpoints
- Watch: Allows you to monitor specific expressions
[Image placeholder: Screenshot of the right debugging panels showing scope variables and call stack]
Setting and Managing Breakpoints
Basic Breakpoints
To set a breakpoint:
- Navigate to the JavaScript file you want to debug
- Click on the line number where you want execution to pause
- A red dot (or blue in Firefox) will appear, indicating an active breakpoint
[Image placeholder: Screenshot showing a red breakpoint dot on a line of JavaScript code]
Conditional Breakpoints
Right-click on a line number and select "Add conditional breakpoint" to create breakpoints that only trigger when specific conditions are met.
[Image placeholder: Screenshot of the context menu showing conditional breakpoint option and the condition input field]
Logpoints
Some browsers support logpoints, which log messages to the console without pausing execution. Right-click a line number and select "Add logpoint."
Pausing on Uncaught Exceptions
One of the most useful debugging features is the ability to automatically pause when your code throws an uncaught exception.
Enabling Exception Breakpoints
- In the Sources panel, look for the "Pause on exceptions" button (usually represented by a pause icon with a hexagonal stop sign)
- Click this button to toggle exception pausing
- You may see options for:
- Pause on all exceptions: Stops for both caught and uncaught exceptions
- Pause on uncaught exceptions only: Stops only when exceptions aren't handled by try-catch blocks
[Image placeholder: Screenshot showing the pause on exceptions button and its dropdown options]
When an Exception Occurs
When the debugger pauses on an exception, you'll see:
- The line where the exception occurred highlighted
- Exception details in the console
- The call stack showing how you reached this point
- Variable values at the time of the exception
[Image placeholder: Screenshot of debugger paused on an exception, showing the highlighted error line and exception details]
Navigating Through Paused Code
Step Controls
When the debugger is paused, you have several options for controlling execution:
- Continue (F8): Resume normal execution until the next breakpoint
- Step Over (F10): Execute the current line and move to the next line in the same function
- Step Into (F11): If the current line contains a function call, step into that function
- Step Out (Shift+F11): Complete the current function and pause at the calling code
[Image placeholder: Screenshot of the step control buttons in the debugger interface]
Examining the Call Stack
The call stack shows the sequence of function calls that led to the current execution point. Click on any function in the stack to:
- View that function's source code
- Examine local variables at that point in execution
- Understand the flow of your program
Continue Reading
Unlock the full course to access all content and examples.