# Essential Chrome Developer Tools for Web Development Beginners
Written on
Chapter 1 Overview of Chrome Developer Tools
Chrome Developer Tools (DevTools) provide a comprehensive suite of features that make debugging and development much smoother for web developers. Its seamless integration with the browser positions it as a top choice for anyone in the field. While advanced capabilities like JavaScript profiling and performance data export are beneficial, it's the smaller, more practical features that truly enhance daily workflows. Below, I've compiled a list of handy tips that I wish I had known earlier.
General Features
- Disable Cache: You can turn off the browser cache by navigating to settings (click the gear icon in the upper right) and selecting "Disable cache," but this option only works while the Web Inspector is open.
- Zoom Functionality: Since the Web Inspector is essentially an HTML page, you can zoom in and out using standard keyboard shortcuts.
- Inspecting the Inspector: You can analyze the inspector itself. Open a Web Inspector and access chrome://inspect in a separate window. Under "Other," you’ll find the open inspector for inspection, which is especially useful for custom styling.
- Custom Styles: The inspector allows for custom styles, as its body has the ID -webkit-web-inspector, enabling unique styling for the inspector itself. Personally, I utilize this feature to increase font size.
- Themes: Beyond custom styles, you can implement complete themes for the inspector.
- Device Overrides: In the settings (click the cogwheel in the Web Inspector's bottom right), you can simulate various devices. This functionality not only changes the user agent string but also adjusts the viewport size to match the selected device, which is incredibly useful for responsive design.
- Print Emulation: Within the overrides section, you can specify a media type for Chrome under "Emulate CSS Media," allowing you to design print versions of pages without switching to the actual print view.
- Keyboard Shortcuts: The shortcuts vary by operating system but can be found under "Shortcuts" in the settings, making it easier to navigate without leaving your development environment.
Console Features
- Always-Available Console: You don't need a separate console panel in DevTools; a console can be displayed at the bottom of any panel with a click of the corresponding button or by pressing ESC.
- Console in Frames: You can execute console commands within the context of an (I) frame. Each console has a dropdown menu set to "<top frame>" by default, allowing you to select frames to run commands.
- Message Persistence: Modify the console settings to retain messages longer by enabling "Preserve log upon navigation," ensuring you can refer back to them.
- Element Selection: Use $() to select individual elements in the JavaScript console via CSS selectors. Example: $('body').
- Multiple Element Selection: Use $$() to select multiple elements with CSS selectors. Example: $$('p.foo').
- Reference Current Element: The $0 reference in the JavaScript console points to the currently selected DOM node in the Elements panel. Example: console.log($0).
- Element Selection History: The console maintains a history of element selections with $1, $2, etc., referencing previously selected elements.
- Expression Memory: The $ _ variable recalls the last evaluated expression, allowing for quick access to results.
- Inspecting Elements: Use the inspect() function to select a specified element in the Elements panel. Example: inspect($2).
- Detailed Logging: The console.dir() method displays a DOM node as a JavaScript object with all its properties, while console.log() shows the content of the node. Example: console.dir($0).
- Counting Events: The console.count() function counts occurrences of specific events, using a string as an identifier.
- String Substitution in Logging: Use placeholders in console.log() for formatted output, such as %s for strings and %d for integers. Example: console.log('At %f, there were %d elements', Date.now(), document.childNodes.length).
- CSS in Logs: Incorporate CSS styles into your console output using the %c placeholder. Example: console.log('%cRed %cLarge', 'color:red', 'font-size:2em').
- Warning Logs: Use console.warn() to display warning messages, complete with an appropriate icon. Example: console.warn('Attention!').
- Error Logs: The console.error() function logs errors, showing them with the relevant icon and stack trace. Example: (function foo(){ console.error('Fail'); })().
- Multiple Log Outputs: You can send multiple outputs in a single console.log() by passing several arguments. Example: console.log('Foo', 42, []).
- Grouping Logs: Group related console messages into collapsible sections using console.group() and console.groupEnd(). You can even nest groups.
- Event Monitoring: Track events without writing code by using monitorEvents(). Example: monitorEvents(document.body, 'click').
- Timing Operations: Utilize built-in timer functions in the console. Start a timer with console.time('Foo') and stop it with console.timeEnd('Foo').
Elements Panel Features
- DOM Search: The Elements panel includes a search function that allows you to quickly find elements throughout the entire DOM. Use the appropriate key combination to open the search.
- Drag and Drop: Rearranging elements in the Elements panel is as simple as dragging them to the desired location.
- Force Element State: Right-click on an element to set its state permanently under "Force Element State."
- Editable Metrics: You can directly edit metric values in the metrics box by double-clicking them, bypassing the CSS altogether.
- Persisting Style Changes: When you make changes to styles or metrics, the Inspector automatically saves them in the corresponding entries in the Sources panel.
- DOM Breakpoints: Set breakpoints for modifications to elements via the context menu under "Break on …" so that the debugger halts when attributes or child elements change.
Network Panel Features
- Sort Requests: In the network panel, requests can be sorted not just by total time but also by specific characteristics like latency or duration.
Debugging Techniques
- Debugger Statement: Use the debugger statement within your JavaScript code to create a breakpoint. Example: if(shouldNeverHappen){ debugger; }.
- Conditional Breakpoints: Set breakpoints that only trigger when a specified JavaScript expression evaluates to true.
- Breakpoint Actions: You can execute any JavaScript expression as a breakpoint action, which allows for logging without halting the program.
- Pretty Print: Deobfuscate minified JavaScript in the Sources panel using the "Pretty Print" button.
- Break on Exception: Use the pause button next to Pretty Print to halt execution when an unhandled exception occurs, making troubleshooting easier.
- Error Stack Traces: Access the stack trace of errors through the Error.stack property, enabling you to log or display them. Example:
try { existiertNicht(); } catch(err) { console.log(err.stack); }
- Watch Expressions: Store frequently used expressions in the "Watch Expressions" section of the Sources panel for easy access.
- XHR and Event Breakpoints: Set breakpoints for XHR requests and events directly in the Sources panel.
Coding Shortcuts
- Quick HTML Page: To rapidly create an HTML page for testing, simply enter data:text/html,<!doctype html> in a new tab.
- Hot Swapping: Changes to JavaScript files can be applied instantly in the browser without reloading the page. Open the file in the Sources panel, modify it, and use the save hotkey.
- Code Snippets: Keep frequently used JavaScript snippets in the DevTools, accessible in the Snippets tab.
- Diffs for Local Modifications: If you’re unsure of what changes have been made to the sources, use the "Local Modifications" option in the context menu for a detailed comparison.
Extensions and Libraries
- LiveReload: This tool is essential for developers, allowing for live updates during development. If you're not using it yet, it’s highly recommended.
- CoffeeConsole 2: A compact console integrated within the Web Inspector for enhanced usability.
- Log Library: A JavaScript library that simplifies formatted console logging and supports basic Markdown.
I invite you to share any additional tips and tricks you may have regarding Chrome Developer Tools!