In the good old days, developers debugged by emitting messages to the command line. The introduction of dedicated debuggers accelerated the error-finding process.
Debugger work centres around breakpoints. They act as an entry point into the debugging session – if code hits them, its execution pauses. The easiest way to add a breakpoint involves the Sources tab, where you click the margin to add a blue rhomboid.
When the relevant line is hit, a yellow insert pops up over the rendered view. Furthermore, the debugger window populates with various bits of information about the current ‘context’. Moving your mouse over any variable opens a pop-up window similar to the one shown in the figure. If the element in question is an object, a tree view will appear instead. It enables you to drill down into the individual variables.
Global variables show up in the Scope node. Click it and be prepared to wait for a second or two. Its population cannot be accomplished instantaneously due to the number of elements, using the Filter textbox at the top of the screen is highly recommended for usability.
As populating the entire state tree is slow, some fields get shown as (…) instead. Double-click any one of these attributes to load its value on the fly. Finally, use the call tree to find out where you are. It lists the methods called to arrive at the execution position.
Placing breakpoints all over the place is inefficient. Analyse the flow of a variable by clicking the three step-over buttons next to the blue Play button. They enable you to run a single line or return from a function while keeping the debugger active during the process. This is helpful when hunting down value changes as an algorithm does its work.
Another neat trick involves the use of conditional breakpoints. Chrome supports half a dozen of them, the table below describes them.
Setting these is done outside of the debugger. In the case of the DOM tree, for example, a node must be selected in the Element view. You can then specify that a debugging session must launch whenever the content of this node changes.
Like most other scientific endeavours, a few ‘best practices’ have arisen over time. The reference card from dzone.com might have an extremely annoying layout, but it does provide an overview of interesting aspects.
Chrome DevTools reference
Chrome’s debugger contains dozens of additional features that we don’t have room to cover in this tutorial. Seasoned web developers are advised to take a look at Google’s official documentation – some of the functionality tends to be a real timesaver.
Sometimes, Chrome’s developer tools simply don’t offer what you might be looking for. If that is the case, you can give a dedicated product like WebStorm a chance. Its debuggers tend to analyse the entire project structure, leading to even more advanced analytical capabilities.
Next page: Working with Device Mode