JavaScript Tutorial

Javascript Debugging

Debugging in JavaScript is the process of finding and fixing errors or bugs in your code. It helps you identify and resolve issues that may be causing unexpected behavior or incorrect output in your JavaScript programs.

The best practice to find out the error is to debug the code. The code can be debugged easily by any web browser like Google Chrome, or Mozilla Firefox.

There are various techniques and tools available for debugging JavaScript code:

Using console.log()

This method displays the result in the console of the browser. If it finds any mistake in the code, it generates an error message.

To open the console, press F12 or right-click on the browser, then click on inspect element. In this panel, go to the second tab with the name Console.

Input:-

Output:-

 We are initializing the variable in the above example and trying to output that variable in the console, it helps in checking the value of any variable.

Using debugger/ breakpoints

Using the debugger/breakpoints

We set breakpoints to examine each line of code step by step. In JavaScript, we can use the debugger keyword to set a breakpoint directly in the code. When the debugger keyword is encountered, it pauses the execution of the program at that point.

To use the debugger, we need to open the browser's developer tools by pressing F12 or right-clicking on the page and selecting "Inspect element". Once the developer tools are open, we can navigate to the "Sources" or "Debugger" tab (depending on the browser) and find the relevant JavaScript file. Then, we can click on the line number where we want to set the breakpoint, and the debugger will pause the code execution at that line.

From there, we can examine the values of variables and step through the code manually, line by line, using the controls provided by the debugger. We can resume the execution of the code when we are ready, usually by clicking a play or continue button.

Using the debugger and breakpoints allows us to gain insights into the code's behavior and identify and fix any issues or errors more effectively.

Input:-

Output:-


Go back to Previous Course