It is common to have errors while writing codes and the errors can be due to syntax or logical. These errors create a lot of ambiguity in the logic and understanding of both users and programmers. There can also be errors in the code which can remain invisible to the programmer’s eye and can create havoc. To identify these errors we need Debuggers that can go through the entire code or program, identify the errors and also fix them.
The debugger keyword is used in the code to force stop the execution of the code at a breaking point and calls the debugging function. The debugger function is executed if any debugging is needed at all else no action is performed.
2) Use of console.log() method
There is another way in which the JS values can be displayed in the debugger window.
3) Setting Break Points:
The console.log() is a good way to debug errors but setting breakpoint is a faster, efficient and better method. In this method, Breakpoints are set in code which stops the execution of code at that point so that the values of variables can be examined at that time.
Here are some advantages of using Breakpoints over console.log() method:
Enter Developers tool section by pressing F12 key and go to sources.
OR set a breakpoint by simply clicking line number shown in the code. In the following image, a breakpoint is set at line 23. (The code used is same as the above example)
As shown in the image, the code stopped at line 23 and showed all the values of variables in the code at that point in time.
Since we can see the value of I was initially 1 and at the breakpoint, it is 5. Hence GeeksForGeeks was printed 5-1=4 times.
Method 3 is contributed by Kartikay Bhutani.
- ES6 | Debugging
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.