debugger

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

debugger 语句会调用任何可用的调试功能,例如设置断点。如果没有调试功能可用,则此语句不会产生任何效果。

语法

js
debugger;

示例

使用 debugger 语句

以下示例显示了插入了 debugger 语句的代码,以便在调用函数时调用调试器(如果存在)。

js
function potentiallyBuggyCode() {
  debugger;
  // 做一些可能会出现错误的检查、单步调试等。
}

当 debugger 被调用时,执行暂停在 debugger 语句的位置。就像在脚本源代码中的断点一样。

打开浏览器及其开发者工具,并显示了调试器面板。该面板展示了如何在 debugger 语句处暂停执行,以便仔细检查变量、作用域、事件等。

规范

Specification
ECMAScript® 2025 Language Specification
# sec-debugger-statement

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
debugger

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

参见