Open In App

What are Some Common Debugging Techniques for JavaScript ?

Last Updated : 22 Mar, 2024
Like Article

JavaScript development can be tricky, and errors are common. But do not worry! There are ways to find and fix these problems.

Some of the common methods to debug JavaScript code are listed below:

Using console.log()

If you want to see messages or check the values of variables while your code is running, you can use console.log(). This function will print out whatever you specify to the console in your web browser. It’s a handy way to keep track of what’s happening in your code and see the values of different things as your program runs.



Example: The below code will explain the use of the console.log() to debug your JavaScript code.

let age = 30;
console.log("Age is: " + age);

Age is: 30

Error Handling (try…catch blocks):

When something unexpected goes wrong in your code, handle it carefully to prevent your program from crashing. You can show helpful messages to users, record the errors for later, and offer alternative solutions to keep things running smoothly.

Example: The below code implements the try/catch block in JavaScript to debug the code.

try {
    let result = JSON.parse(invalidJSONString);
} catch (error) {
    console.error("Error parsing JSON:", error.message);


Error parsing JSON: invalidJSONString is not defined.

Using debugger and developer tools

If you are having trouble with your code and need to take a closer look, you can use the debugger tool in your browser’s developer tools. This tool lets you stop the code from running at a specific line so you can check what’s going on. You can then look at the values of different things in your code and see how your program is working. It’s like hitting a pause button to investigate what’s happening in your code.

The stepwise approach to use debugger and the developer tools is written below:

Step 1: Imagine you have a webpage with a JavaScript bug and open the developer tools (F12).


Opening DevTools

Step 2: In the “Sources” panel, you can see all the JS files of the site.


Opening a JS file in Sources Tab

Step 3: You suspect the bug lest say is line 4, so you click on the line number 4 to set a breakpoint.


Clicking the line no. to add breakpoint

Step 4: You refresh the webpage, and the browser pauses right before executing line 4 (due to the breakpoint).


The browser paused the execution and fill the sidebar

Step 5: The “Scope” pane shows you the values of variables used on line 4. You see an unexpected value and identify the root cause of the bug.


All variables used in that block

Step 6: You will find various options in the right sidebar in the Sources tab to step through the code and, examining the variables value at each step.


Debugger Buttons

Step 7: Once you understand the issue, you remove the breakpoint and resume execution to test your fix.


Clicking the line no. to remove breakpoint

Similar Reads

Debugging in JavaScript
In this article, we will learn about debugging in Javascript. Debugging is the term that means examining the code, finding the error in the code, and fixing that error. It is common to have errors while writing codes and the errors can be due to syntax or logic. These errors create a lot of ambiguity in the logic and understanding of both users and
4 min read
Top 10 Best JavaScript Debugging Tools for 2024
JavaScript is one of the most popular programming languages in the world and the web can’t be complete without this language. But due to its loosely typed nature and various other factors, like all the other languages, JavaScript is also prone to errors and bugs. Sometimes, it can be tricky to find and fix the issues, and can take a lot of time. Ho
8 min read
Common techniques for working with immutable data in Redux?
Immutable data is a fundamental concept in Redux. By ensuring that state changes are immutable, Redux helps to maintain a predictable state tree and simplifies debugging by enabling easier tracking of changes. In this article, we'll see the following techniques and best practices for working with immutable data in Redux. Table of Content 1. Spread
3 min read
What are some common patterns for handling pagination in Redux?
Pagination in Redux involves managing the state to store the current page number and total number of pages, dispatching actions to fetch specific data pages, and updating the Redux store accordingly for a seamless user experience. Some common patterns for handling pagination in Redux:Storing Pagination Metadata: Store metadata such as the current p
2 min read
What are Some Common Libraries/Tools for Handling Authentication in Redux ?
Authentication is a crucial aspect of many web applications, ensuring that only authorized users can access certain features or data. When building Redux-powered applications, integrating authentication can be streamlined by using specialized libraries and tools listed below. Table of Content Using Redux-AuthUsing Redux-SagaUsing Redux-ThunkUsing R
2 min read
What are some Common Libraries/Tools for State Normalization in Redux ?
State normalization in Redux involves organizing the state in a flat structure to simplify data access and manipulation. Several libraries and tools provide state normalization in Redux applications, streamlining state management and improving performance. There are several common libraries/tools for state normalization in redux which are as follow
3 min read
Git- Debugging
Like traditional word editors, we cannot undo changes simply. We have various git commands available to debug and undo changes. We will discuss such commands in this article. git bisect: This command uses the binary search algorithm to find out the commit that introduced a bug. It is used by first telling it "bad" commit i.e. commit that is known t
3 min read
Debugging and Testing of a Node.js Application
While building a big project working in a team, continuous changes being in code base are going to achieve optimum results. So if someone writes code and because of which an already existing feature gets broken, for that purpose we use software testing. Keeping your project tested automatically we have tests, every time we run the project, we can c
4 min read
ES6 Debugging
We have earlier seen the introductory concepts of ES6.. Debugging is the act of looking throughout the code, understanding what it is doing, and figuring out why the program is not acting as expected. In the below example, the console.log() function is used for code debugging. To understand what is going on in the program at each step, we are figur
2 min read
React Native Debugging
Debugging is very important for building applications and removing errors. A good knowledge of debugging techniques allows for the faster and efficient development of software. Here we are going to discuss a few debugging techniques in React Native. We will be using expo-cli to develop, run and debug our applications which is one of the simplest an
3 min read