Debugging in JavaScript: Day 8

In this article you will learn about debugging in JavaScript..

Introduction

I highly recommend reading the previous parts:

Debugging in JavaScript

All programmers know that writing code without a debugger is difficult, because when you are testing it's easy to check the values one by one of each statement. But in JavaScript code there is no line by line debugging possible. If there is an error in code then it might be a syntax error or a runtime error, it's difficult to determine, because in the output if the code is correct then the output will be displayed otherwise it shows nothing. And there is nothing to display like an error in a line number or something like that or an error message to display.

Searching for an error in JavaScript code is difficult. It's not easy to debug the JavaScript code but now all browsers provide a debugger. Use the following procedure in a Chrome Browser to learn about debugging.
  1. Open the Chrome Browser.
  2. Click on Menu.
  3. Select More Tools.
  4. In that sub menu then select JavaScript Console.
  5. It will open the debugger. (Press F12 to open the debugger).

debugger

Select JavaScript Console

You will now see how to open the debugger in the Chrome browser. You can log the result and show something in the debugger window in JavaScript using the console.log() method. the following example clarifies this about debugging.

Example For console.log() Method

  1. <!DOCTYPE html>  
  2. <html>  
  3. <title>Article By Jeetendra</title>  
  4. <head>  
  5. </head>  
  6. <body>  
  7. <script type="text/javascript">  
  8. document.write("Debugging in JavaScript</br>");  
  9. function ExceptHand()  
  10. {     
  11.     try  
  12.     {  
  13.         if(true)  
  14.         { throw("Example of Debugger in JavaScript Using console.log() Method");}  
  15.     }  
  16.     catch(error)  
  17.     {  
  18.         console.log(error);  
  19.     }  
  20. }  
  21. ExceptHand();  
  22. </script>  
  23. </body>  
  24. </html>  
Output

Output


In the preceding example you can throw the error message and it is caught in the catch block and is written to the log and that will be displayed in the debugger window as in the preceding image.

The debugger keyword

This debugger code is used for setting breakpoints in JavaScript code for checking the values. It stops the JavaScript until the debugger keyword and after checking the values you can resume the execution (Press F8). But if the debugger is on then it will show the debugging otherwise it will show the result. If debugging is on then it will stop execution until that line before executing the next line.

Example of debugger keyword
  1. <!DOCTYPE html>  
  2. <html>  
  3. <title>Article By Jeetendra</title>  
  4. <head>  
  5. </head>  
  6. <body>  
  7. <script type="text/javascript">  
  8. document.write("Debugging in JavaScript</br>");  
  9. function ExceptHand()  
  10. {     
  11.     try  
  12.     {  
  13.         var a = 10;  
  14.         var b = 20;  
  15.         var c = a + b;  
  16.         debugger;  
  17.         if(true)  
  18.         {             
  19.             document.write("using debugger keyword");  
  20.         }  
  21.     }  
  22.     catch(error)  
  23.     {  
  24.         console.log("log message");  
  25.     }  
  26. }  
  27. ExceptHand();  
  28. </script>  
  29. </body>  
  30. </html>  
Output

When you execute the preceding JavaScript code it will be shown as in the following image if the debugger is on otherwise it will directly print the document print messages. In the following image you can see the debugger window -> source tab and you can see the JavaScript code and a highlighted line in the debugger, this is the breakpoint for debugging.

debugging

In the preceding image, in the right side, there are multiple options for using debugging mode. Click on the Watch Expression then click on the (+) sign. It will then show a text box to find the expression values. Then type "c" into the TextBox then press Enter. It will show output like "c: 30" in addition to the two variables a and b. You can see it in the second image on the right side. I highlighted that in a Red color box.

variables a and b

Finally you get the expression value. Press F8 or press the resume button, it will execute the next line. Here the condition is checked and it prints a message and it will show output as in the following image.

show output

Summary

I hope you understood the concepts of debugging in JavaScript and that this is useful for all readers. If you have any suggestion regarding this article then please contact me.