Yesterday everything worked, but today it does not work / The code does not work as intended
or
Debugging
What is the debugging process? What it is?
The debugging process consists in the fact that we stop the execution of the script in any place, we look at what is in the variables, in the functions, analyze and move to other places; look for those places where the behavior deviates from the correct.
The example with Сhrome will be considered, but the code can be debugged in any other browser and even in the IDE .
Open the developer tools. Usually they open by pressing the F12 button or in the Инструменты → Инструменты Разработчика menu. Select the Sources tab

Numbers denoted by:
- The hierarchy of files connected to the page (js, css and others). Here you can select any script for debugging.
- The code itself.
- Additional features to control.
In section 2, on the left, on any line, you can click the LMB , thereby setting a breakpoint (breakpoint - breakpoint). This is where the debugger will automatically stop running javascript as soon as it comes to it. The number of breakpoints is unlimited. You can put everywhere and a lot. The image above is marked in green.
In the stopped code, you can see the current values of variables, execute various commands, etc.
And in the Breakpoints tab you can:
- Temporarily disable breakpoint (s)
- Remove breakpoint (s) if not needed
- Quickly go to the place of the code, where there is a breakpoint by clicking on the text.
We start debugging
In this case, because the function is executed immediately when the page is loaded, then to activate the debugger it is enough to reload it. Otherwise, the activation requires the execution of an action in which the execution of the desired part of the code will occur (click on the button, fill the input with data, move the mouse and other actions)
In this case, after reloading the page, the execution will "freeze" on line 4:

Tab Watch - shows the current values of any variables and expressions. At any time here you can click on + , enter the name of any variable and see its value in real time. For example, data or nums[0] , or nums[i] and item.test.data.name[5].info[key[1]] , etc.
Call Stack tab - call stack, all nested calls that led to the current code point. At the moment the debugger is in the getSum function, line 4.
Scope Variables tab - variables. At the current moment, the lines below number 4 have not yet been completed, so sum and output are equal to undefined .
Local shows variable functions: declared via var and parameters. In Global - global variables and functions.
Process
For the process itself controls are used (see the image above, highlighted with a green rectangle)
( F8 ) - continue execution. Resumes script execution from now. If there are no other breakpoints, the debugging ends and the script continues. Otherwise, the work is interrupted at the next breakpoint.
( F10 ) - takes one step without going inside the function. Those. if there is a function on the current line, and not just a variable with a value, then when you click this button, the debugger will not go inside it.
( F11 ) - takes a step. But unlike the previous one, if there is a nested call (for example, a function), then it goes inside it.
( Shift + F11 ) - executes commands until the end of the current function. Convenient if you accidentally entered a nested call and you need to quickly exit it, without completing debugging.
- disable / enable all breakpoints
- enable / disable automatic stop on error. If enabled, then if an error occurs in the code, the script will stop automatically and you can look in the debugger for the current values of the variables, analyze and take corrective measures.
...
So, in the current code you can see the value of the input parameter:
data = "23 24 11 18" - a string with data separated by a spacenums = (4) ["23", "24", "11", "18"] is an array that is obtained from the input variable.
If we press F10 2 times, we will be on line 7; in the tabs Watch , Scope > Local and in the page with the code we will see that the variable variable has been initialized and the value is 0.
If we now press F11 , then we’ll go inside the nums.forEach closure nums.forEach

By pressing F10 now until the loop is over, it will be possible to observe how the values of num and sum constantly change at each iteration of the loop. Thus, we can trace, step by step, the whole process of changing any variables and values at any stage that interests us.
Further pressing F10 will move the code line to lines 11, 12 and, finally, 15.
Additionally
You can force a stop without any breakpoints if you write the debugger keyword directly in the code:
function getSum(data) { ... debugger; // <-- отладчик остановится тут ... }
If you press RMB on the line with breakpoint, this will allow you to further fine-tune the condition under which you must stop at this point. In the function above, for example, you only need to stop when sum exceeds 20.

This is convenient if the stop is needed only at a certain value, and not always (especially in the case of cycles).
More information about the capabilities of tools such as Chrome - can be read here.
2 additional
Forced debugging can be triggered by an event occurring on the page / elements. This is useful if you do not know where the processing function is located.
Chrome example:
Press F12 , go to the Sources tab and in the control functions we see the Event Listener Breakpoints tab, in which you can set any events for which the script execution will be stopped as a trigger. In the image below, an item on the event of onchange elements is onchange .

For Firefox :
If the function is inline, for example
<input type="checkbox" onchange="testFunction(this);" />
then you can go to the Inspector, find the same element in which the event is registered and find the em icon next to it:

Clicking on it, according to developer.mozilla.org/ru/docs, you can see the lines:

where you can see the events hung on the element, script, line, the ability to click on a pause, etc.
In other cases, and also if the pause button is not found, then on the Debugger tab (debugger) you need to find an arrow, when you hover over it that says "Events". There must be an event of the selected item.
But there are no useful tabs like Chrome unfortunately for Firefox .
