codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🌐

JavaScript

43 / 65 topics
39JavaScript try...catch...finally40JavaScript throw Statement41JavaScript Error Objects42JavaScript "use strict"43Debugging JavaScript
Tutorials/JavaScript/Debugging JavaScript
🌐JavaScript

Debugging JavaScript

Updated 2026-05-12
30 min read

Debugging JavaScript

In the world of software development, debugging is an essential skill that helps developers identify and fix errors in their code. Whether you're a beginner or an experienced developer, understanding how to effectively debug your JavaScript applications can significantly improve your productivity and reduce frustration.

In this tutorial, we'll explore various techniques for debugging JavaScript using browser developer tools, breakpoints, and the debugger keyword. By the end of this guide, you'll be equipped with the tools and knowledge needed to efficiently identify and resolve issues in your JavaScript code.

Introduction

Debugging is the process of finding and fixing errors or bugs in your code. In JavaScript, debugging can be particularly challenging due to its dynamic nature and the wide variety of environments (browsers, Node.js) where it can run. However, modern web browsers come equipped with powerful developer tools that make debugging a breeze.

In this section, we'll focus on using browser developer tools, setting breakpoints, and using the debugger keyword to step through your code and identify issues.

Using Browser Developer Tools

Modern web browsers like Chrome, Firefox, Safari, and Edge provide robust developer tools that help you debug JavaScript applications. These tools allow you to inspect variables, evaluate expressions, set breakpoints, and step through your code line by line.

Accessing Developer Tools

To access the developer tools in most modern browsers:

  1. Open your web application.
  2. Right-click anywhere on the page and select "Inspect" or "Inspect Element."
  3. Alternatively, you can press Ctrl + Shift + I (Windows/Linux) or Cmd + Option + I (Mac).

Once the developer tools are open, navigate to the "Sources" tab. This is where you'll find all the debugging features.

Setting Breakpoints

Breakpoints allow you to pause your code execution at specific lines, enabling you to inspect variables and evaluate expressions at that point in time. Here's how you can set breakpoints:

  1. Open the "Sources" tab in the developer tools.
  2. Find the JavaScript file you want to debug (usually listed under the "Sources" panel on the left).
  3. Click on the line number where you want to set a breakpoint. A red dot will appear, indicating that a breakpoint has been set.

You can also right-click on a line number and select "Toggle Breakpoint" or use the keyboard shortcut F9 to toggle breakpoints.

Stepping Through Code

Once you've set breakpoints, you can start debugging by triggering the code execution. Here are some common actions you can perform:

  • Resume script execution (F8): Continues the execution until the next breakpoint is hit.
  • Step over next function call (F10): Executes the current line and moves to the next line in the same scope.
  • Step into next function call (F11): If the current line contains a function call, it will step into that function. Otherwise, it behaves like "Step over."
  • Step out of current function (Shift + F11): Executes the remaining lines of the current function and returns to the calling line.

Inspecting Variables

While your code is paused at a breakpoint, you can inspect variables by hovering over them or using the "Scope" panel. This panel shows all the local and global variables available at that point in time.

Using the debugger Keyword

The debugger keyword is a built-in JavaScript statement that allows you to set breakpoints directly within your code. When the JavaScript engine encounters this statement, it will pause execution, just like when a breakpoint is hit in the developer tools.

Basic Usage

Here's a simple example demonstrating how to use the debugger keyword:

script.js
1function add(a, b) {
2debugger; // This will pause execution here
3return a + b;
4}
5
6add(2, 3);

When you run this code in your browser and open the developer tools, execution will pause at the debugger statement. You can then inspect variables and step through the remaining lines of code.

Practical Example

Let's walk through a practical example that demonstrates how to use browser developer tools, breakpoints, and the debugger keyword to debug a more complex JavaScript application.

Consider the following code snippet:

app.js
1function calculateTax(price, taxRate) {
2if (price < 0 || taxRate < 0) {
3 throw new Error("Price and tax rate must be non-negative");
4}
5
6const taxAmount = price * (taxRate / 100);
7return price + taxAmount;
8}
9
10function displayTotal(price, taxRate) {
11try {
12 const total = calculateTax(price, taxRate);
13 console.log(`The total cost is $${total.toFixed(2)}`);
14} catch (error) {
15 console.error(error.message);
16}
17}
18
19displayTotal(100, 5);

Debugging Steps

  1. Open the developer tools and navigate to the "Sources" tab.
  2. Set a breakpoint on the debugger statement inside the calculateTax function.
  3. Run the code by opening the console or using an HTML file that includes this script.
  4. When execution pauses at the breakpoint, inspect the variables:
    • price: Should be 100
    • taxRate: Should be 5
    • taxAmount: Should be 5 (calculated as 100 * 5 / 100)
  5. Step over to the next line and inspect the return value.
  6. Continue execution until it reaches the displayTotal function, where the total cost is displayed in the console.

Expected Output

Output
The total cost is $105.00

Summary

In this tutorial, we covered how to debug JavaScript using browser developer tools, breakpoints, and the debugger keyword. Here are the key takeaways:

  • Browser Developer Tools: Accessible via right-click "Inspect" or keyboard shortcuts. Use the "Sources" tab for debugging.
  • Breakpoints: Set by clicking on line numbers or using the keyboard shortcut F9. Useful for pausing execution and inspecting variables.
  • Stepping Through Code: Use F8, F10, F11, and Shift + F11 to control code execution.
  • Inspecting Variables: Hover over variables or use the "Scope" panel in the developer tools.
  • debugger Keyword: A built-in statement that pauses execution, allowing you to set breakpoints directly in your code.

Mastering these debugging techniques will greatly enhance your ability to identify and fix issues in your JavaScript applications, making you a more efficient and effective developer.

What's Next?

Now that you've learned how to debug JavaScript using various tools and techniques, the next step is to explore JavaScript Callbacks. Callbacks are functions passed as arguments to other functions and are widely used in asynchronous programming. Understanding callbacks will help you write more robust and responsive applications.

Stay tuned for our upcoming tutorial on "JavaScript Callbacks" where we'll dive deeper into how callbacks work and how to use them effectively in your code!


PreviousJavaScript "use strict"Next JavaScript Callbacks

Recommended Gear

JavaScript "use strict"JavaScript Callbacks