1. javascript
  2. /basics
  3. /debugging

Understanding Debugging in Javascript

Introduction to Debugging

Debugging can sometimes be a daunting task. A "bug" in programming refers to an error, mistake, or malfunction in the code. The term "bug" dates back to the 1940s when a technician found an actual bug, a moth, stuck in a relay of a computer. The term then evolved to refer to any problem in the code.

Simply put, debugging is the process of identifying and fixing errors in your code. When you encounter an error, the first step is to identify the source of the problem. This can be done by reading the error message and looking at the line of code where the error occurred. Once you have identified the source of the problem, you can then start to think about how to fix it.

It's important to remember that debugging is an iterative process. You may need to try multiple approaches before finding the solution to the problem. And sometimes, it's not even about finding the error, but about understanding why the code is behaving in a certain way.

Using the Developer Tools in Browsers

Each browser has its developer tools that can be used for debugging Javascript code. The most commonly used browsers are Google Chrome, Mozilla Firefox, and Microsoft Edge. Here's a quick overview of how to open the developer tools in each browser:

  • Google Chrome: Right-click on the page and select "Inspect" or use the shortcut key Ctrl + Shift + I (Windows) or Cmd + Shift + I (Mac).

  • Mozilla Firefox: Right-click on the page and select "Inspect Element" or use the shortcut key Ctrl + Shift + I (Windows) or Cmd + Shift + I (Mac).

  • Microsoft Edge: Right-click on the page and select "Inspect Element" or use the shortcut key F12.

Once you have the developer tools open, you can start using the various panels to debug your code.

Using the Sources Panel

The Sources panel is a powerful tool that allows you to view and debug your Javascript code. It displays all of the files that make up your website, including the HTML, CSS, and Javascript files. Here are some instructions on how to use the Sources panel to navigate through your code and set breakpoints:

  1. Open the developer tools in your browser (Chrome: Ctrl + Shift + I, Firefox: Ctrl + Shift + I, Edge: F12).

  2. Go to the Sources tab.

  3. Navigate to the file that you want to debug by clicking on the file name or by searching for it.

  4. Set a breakpoint by clicking on the line number.

  5. Reload the page and the code execution will stop at the breakpoint, allowing you to inspect the state of the variables and the call stack.

Here are some examples of how you can use the Sources panel:

  • Let's say you have a file called "main.js" that is causing an issue on your website. By using the Sources panel, you can navigate to that file and see the code that is causing the problem. You can also use the search function to find specific lines of code or functions.

  • Another example is when you have an external library or a framework like React or Angular in your application. With the help of the Sources panel, you can navigate through the library's code, set breakpoints, and inspect the state of the variables and the call stack. This will help you understand how the library is working under the hood, and how it's interacting with your code.

Setting Breakpoints

Breakpoints are a powerful tool that allows you to pause the execution of your code at a specific line. This can be useful when trying to understand how a specific part of your code is behaving or if there are any issues with that part of the code.

Here's an example of how you can use breakpoints to debug a function that performs a calculation based on user input:

  • Open the developer tools in your browser

  • Navigate to the file where the function is defined

  • Locate the line where the calculation is happening and set a breakpoint

  • Interact with the application and trigger the function

  • Use the developer tools to inspect the values of the variables at each step of the calculation

function calculateDiscount(price, discount) {
    let discountedPrice = price - (price * (discount / 100));
    console.log(discountedPrice);
    return discountedPrice;
}

In this example, you can set a breakpoint at the line where the calculation is happening and use the developer tools to inspect the values of the price and discount variables at each step of the calculation. This will allow you to understand how the function is behaving and if there are any issues with the calculation.

You can use the debugger statement directly in your code to pause the execution of the code at that point.

function calculateDiscount(price, discount) {
    let discountedPrice = price - (price * (discount / 100));
    debugger;
    console.log(discountedPrice);
    return discountedPrice;
}

By doing this, the code execution will stop at the line where the debugger statement is placed. This can be useful when you want to set a breakpoint in a specific place in your code, but don't have access to the browser's developer tools or when you want to test your code in different environments.

Remember to remove the breakpoints when you're done debugging because they can slow down the execution of the code.

Logging with console.log()

The console.log() is a commonly used tool for logging information and understanding how your code is behaving. Here's a step-by-step guide on how to use console.log() to log information:

  • Open the developer tools in your browser (see the "How to use the developer tools in different browsers" section for instructions on how to do this).

  • In your Javascript code, use the console.log() method to log information.

  • Check the console in the developer tools to see the logged information.

Here's an example of how you can use console.log() to log information about a user object:

let user = { name: "John", age: 30, email: "[email protected]"};
console.log("User data: ", user);

This will output the user object in the console, allowing you to inspect its properties and values.

Debugging in IDEs

Integrated Development Environments (IDEs) provide a more comprehensive development environment and include many features that can make debugging easier. Some popular IDEs for Javascript development include Visual Studio Code, WebStorm, and Atom.

These IDEs have built-in debugging features that allow you to set breakpoints, step through code, and inspect variables. They also have the ability to connect to the browser's developer tools, providing even more powerful debugging capabilities.

For example, Visual Studio Code has a built-in debug console, which allows you to interact with your code while it's running and it also has a live variable viewer that displays the current value of a variable as you step through your code. Also, WebStorm has a built-in debugger with a graphical interface that allows you to set breakpoints, step through code, and inspect variables.

Conclusion

Coding isn't about typing fast and it's not magic like the usual portrayal in pop culture. Sometimes, it requires a lot of effort to locate a certain problem, especially in big web applications. Debugging can be a time-consuming task, and it's not uncommon to spend hours or even days trying to find and fix a single error.

It's essential to remember that coding is a process, and debugging is a crucial part of that process. Don't be afraid to experiment with different approaches and tools when debugging your code. With practice, you'll be able to quickly identify and fix errors in your code, making your development process more efficient.