Let us attending at some simple JS cipher with a bug and try to fix that appliance Dev Tools.
Run the aloft cipher and you should able to see a folio with two argument fields and a on. Now blazon in two numbers in the argument fields and bang on the Add on. We can see that the appliance doesn’t do what is intended. It doesn’t affectation the sum. We will now fix this bug.
Breakpoints can be acclimated back we appetite to abeyance the beheading of cipher at any assertive band of code. We can again ysis what ethics are stored in any capricious at that point of time.
Now go advanced and try to add two numbers. Back we bang on Add on, the cipher beheading is paused and the band with onClick action is highlighted. Back it’s an accident based breakpoint, DevTools pauses on the band which contains that event’s handler.
Stepping through the cipher helps us to assassinate the cipher band by band and we can ysis the adjustment in which the cipher is executing. We can do three altered types of accomplish while activity through a code. In the Sources animate of the Developer Tool, we can acquisition the ons for footfall into, footfall out, and footfall over.
Based on the acceptance of the area of the bug, we can set a breakpoint afterpiece to that allotment of the cipher rather than aloof putting on an event.
We can set a breakpoint on a assertive band of cipher which is alleged a ‘line-of-code breakpoint.’
Just bang on the band cardinal on the larboard ancillary of the code. The blush of that cardinal changes to dejected which indicates that the breakpoint is set. Now during the run, the beheading will abeyance at that line-of-code.
When a breakpoint is set, we can calmly yze what is stored in a capricious at that moment of time. Let us set a breakpoint at band 11 in our cipher and see how the capricious ethics change. We can ysis that in the Local area of the Developer Tools.
To affirm whether this is the bug or not we can use three altered methods to acquisition out how the capricious sum is actuality evaluated.
Console.log() –console.log() is acclimated in the cipher to ysis how the ethics of the variables are alteration over time. But this involves chiral alteration of the cipher and we charge to abode assorted curve of console.log() to acquisition area the bug is.
For acknowledging this bug, let us acquisition the typeof sum capricious through the console.
Type in typeof(sum) in the animate in debugging mode. We should see the blazon as a string, which confirms our acceptance that the capricious sum is actuality evaluated as a cord but not a number.
In the Sources panel, aggrandize the Watch section. Now bang on the sym to add an expression.
Type typeof(sum)and hit enter. We will be able to see that the blazon of sum is “String.” Hence, this additionally confirms our acceptance that sum is actuality evaluated as a string.
Since we accept articular the bug now, we aloof accept to catechumen the cord inputs to a cardinal and accredit the absolute of numbers to the sum variable. We can change the account on band 13 to one of the following:
We can adapt the cipher in the Sources animate anon and save appliance Ctrl S to ysis the fix.
Now resume the execution.
We can conciliate the breakpoints appliance the Conciliate Breakpoints advantage (Ctrl F8) in the Sources animate of Developer Options