Setting and Managing Watch Expressions
You'll learn how to create watch expressions that automatically display variable and expression values as you pause and step through your code during debugging.
What Are Watch Expressions?
Watch expressions let you monitor specific variables or JavaScript expressions while debugging. Instead of repeatedly typing console.log(myVariable) or manually inspecting values in the console, you define expressions once—and the debugger continuously evaluates and displays them every time code execution pauses at a breakpoint.
Think of watch expressions like a dashboard in your car. Rather than stopping to check your speed, fuel, and temperature separately, all critical metrics are always visible. Similarly, watch expressions keep your important values in view as you step through code.
Creating a Watch Expression
In Chrome DevTools' Sources panel, locate the Watch pane (usually on the right sidebar). Click the + button and type any valid JavaScript expression:
- Simple variables:
userId - Object properties:
user.email - Array elements:
items[0] - Calculations:
width * height - Function calls:
getStatus() - Complex expressions:
array.filter(item => item.active).length
Why They're Essential
When stepping through complex code with nested functions or loops, manually inspecting values becomes tedious. Watch expressions automatically re-evaluate at each pause point, showing you how values change as execution progresses. They're particularly valuable when debugging asynchronous code or tracking how state transforms across multiple function calls.
Managing Your Watches
You can add multiple expressions, delete unwanted ones (hover and click the ×), or temporarily disable them. If an expression is undefined or throws an error at the current execution point, DevTools displays that status—helping you understand scope and timing issues.
Key Takeaway: Watch expressions provide real-time visibility into variable values and custom expressions throughout your debugging session, eliminating repetitive manual inspection and keeping critical data always visible.