Set a breakpoint so that you can pause your code in the middle of its execution.
To learn how to set breakpoints, see Pause Your Code With Breakpoints.
Check values when paused
While the execution is paused, the debugger evaluates all variables, constants,
and objects within the current function up to a breakpoint. The debugger shows
the current values inline next to the corresponding declarations.
Once your code is paused, step through it, one expression at a time, investigating control flow and property values along the way.
Step over line of code
When paused on a line of code containing a function that's not relevant to the
problem you're debugging, click Step overstep_over to execute the function
without stepping into it.
You're paused on A. Once you've pressed
step_over, DevTools executes all
the code in the function that you're stepping over, which is B and C.
DevTools then pauses on D.
Step into line of code
When paused on a function call that is related to the problem you're debugging,
click Step into
to
investigate that function.
You're paused on A. By clicking step_out,
DevTools executes the rest of the code in getName(), which is just B in
this example, and then pauses on C.
Run all code up to a certain line
When debugging a long function, there may be a lot of code that is not related
to the problem you're debugging.
You could step through all the lines, but that can be tedious. You could set
a line-of-code breakpoint on the line you're interested in and then press
resume, but there's a
faster way.
Right-click the line of code that you're interested in, and select
Continue to here. DevTools runs all of the code up to that point, and then
pauses on that line.
To ignore all breakpoints and force your script to resume execution, click and
hold Resume Script Executionresume
and then select Force script executionplay_arrow.
When working with web workers or service workers, click a context listed in
the Threads pane to switch to that context. The blue arrow icon represents
which context is selected.
For example, suppose that you're paused on a breakpoint in both your main script
and your service worker script. You want to view the local and global properties
for the service worker context, but the Sources panel is showing the main script
context. By clicking on the service worker entry in the Threads pane, you'd be
able to switch to that context.
Step through comma-separated expressions
You can debug minified code by stepping through comma-separated expressions. For
example, consider the following code:
The Debugger steps through such expressions just the same.
Stepping through a comma-separated expression.
Therefore, the stepping behavior is identical:
Between minified and authored code.
When using source maps to debug the minified code in terms
of the original code. In other words, when you see semicolons, you can always
expect to step through them even if the source you're debugging is minified.
View and edit local, closure, and global properties
While paused on a line of code, use the Scope pane to view and edit the
values of properties and variables in the local, closure, and global scopes.
To observe the behavior of a function and re-run it without having to restart
the entire debugging flow, you can restart the execution of a single function
when this function is paused. In other words, you can restart the function's
frame in the call stack.
Notice that the debugger shows you the current value next to function
declaration: value = 1.
The current value next to function declaration.
Restart the bar() frame.
Restarting the bar() frame.
Step through the value increment statement by pressing F9.
Incrementing current value.
Notice that the current value increases: value = 2.
Optionally, in the Scope pane, double-click the value to edit it and set the desired value.
Editing the value in the Scopes pane.
Try restarting the bar() frame and stepping through the increment statement
several more times. The value continues to increase.
Restarting the bar() frame again.
Restart the foo() frame in the Call Stack.
Restarting the foo() frame.
Notice that the value is 0 again.
Resume script execution (F8) to complete this tutorial.
Show ignore-listed frames
By default, the Call Stack pane shows only the frames that are relevant to
your code and omits any scripts added to
settingsSettings > Ignore List.
Key Point DevTools implements this "Async Stack Tagging" feature based on the
console.createTask() API method. It's up to frameworks to implement the API.
For example, Angular supports this feature.
Copy stack trace
Right-click anywhere in the Call Stack pane and select Copy stack trace
to copy the current call stack to the clipboard.
Group authored and deployed files in the file tree
When developing web applications using frameworks (for example,
React or Angular), it can be
difficult to navigate sources due to the minified files generated by the build
tools (for example, webpack or
Vite).
To help you navigate sources, the Sources > Page pane can group the
files into two categories:
codeAuthored. Similar to the source
files you view in your IDE. DevTools generates these files based on source maps
provided by your build tools.
Deployed. The actual files that the browser reads. Usually these files are minified.
To enable grouping, click more_vert >
Group files by Authored/Deployed option,
under the menu at the top of the file tree.
To help you focus only on the code you create, the Sources > Page pane
grays out all scripts or directories added to
settingsSettings > Ignore List by default.
To hide such scripts altogether, select Sources > Page > more_vert > Hide ignore-listed sources Experimental..
Ignore a script to skip it while debugging. When ignored, a script is
obscured in the Call Stack pane, and you never step into the script's
functions when you step through your code.
For example, suppose you're stepping through this code:
A is a third-party library that you trust. If you're confident that the
problem you're debugging is not related to the third-party library, then it
makes sense to ignore the script.
Ignore a script or a directory from the file tree
To ignore an individual script or an entire directory:
In Sources > Page, right-click a directory or a script file.
If you didn't hide ignore-listed sources, you can select
such a source in the file tree and, on the
warning warning banner, click
Remove from ignored list or Configure.
If you find yourself running the same debug code in the Console over and over,
consider Snippets. Snippets are executable scripts that you author, store, and
run within DevTools.
Click Add Expressionadd
to create a new watch expression.
Click Refreshrefresh to refresh
the values of all existing expressions. Values automatically refresh while
stepping through code.
Hover over an expression and click Delete Expression
Delete expression
to delete it.
Inspect and edit scripts
When you open a script in the Page pane, DevTools shows you its contents in the Editor pane. In the Editor pane, you can browse and edit your code.
Additionally, you can override the contents locally or create a workspace and save the changes you make in DevTools directly to your local sources.
Make a minified file readable
By default, the Sources panel pretty-prints minified files. When pretty-printed, the Editor may show a single long code line in multiple lines, with - to indicate that it's the line continuation.
When fixing a bug, you often want to test out some changes to your JavaScript.
You don't need to make the changes in an external browser and then reload the
page. You can edit your script in DevTools.
To edit a script:
Open the file in the Editor pane of the Sources panel.
Make your changes in the Editor pane.
Press Command+S (Mac) or Ctrl+S
(Windows, Linux) to save. DevTools patches the entire JavaScript file into
Chrome's JavaScript engine.
Press Command / Control + S to apply
changes. The debugger restarts the function automatically.
Continue the execution.
Watch the video to learn this workflow.
In this example, the addend1 and addend2 variables initially have an
incorrect string type. So, instead of adding numbers, the strings are
concatenated. To fix it, the parseInt() functions are added during live
editing.
Search and replace text in a script
To search for text in a script:
Open the file in the Editor pane of the Sources panel.
To open a built-in search bar, press Command+F (Mac) or
Ctrl+F (Windows, Linux).
In the bar, enter your query.
[画像:Search.]
Optionally, you can:
Click Match Case to make your query case-sensitive.
Click Use Regular Expression to search using a RegEx expression.
Press Enter. To jump to previous or next search result, press the
up or down button.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2017年01月04日 UTC."],[],[],null,[]]