控制台 API

控制台 API 提供允许开发者履行调试任务的功能,譬如:在代码中设定点日志消息或变量值,或计时完成操作需要多长时间。

概念和用法

The Console API started as a largely proprietary API, with different browsers implementing it, albeit it in inconsistent ways. The Console API spec was created to define consistent behavior, and all modern browsers eventually settled on implementing this behavior — although some implementations still have their own additional proprietary functions. Find out about these at:

Usage is very simple — the console object — available via window.console ,或 WorkerGlobalScope.console in workers; accessible using just console — contains many methods that you can call to perform rudimentary debugging tasks, generally focused around logging various values to the browser's Web 控制台 .

By far the most commonly-used method is console.log , which is used to log the current value contained inside a specific variable.

接口

console
Provides rudimentary debugging functionality, including logging, stack traces, timers, and counters.

范例

let myString = 'Hello world';
// Output "Hello world" to the console
console.log(myString)
					

控制台参考页 了解更多范例。

规范

规范 状态 Comment
控制台 API 实时标准 初始定义。

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
Console Chrome 1 Edge 12 Firefox 2 IE 8
8
In Internet Explorer 8 and 9, the console object is undefined when the developer tools are not open. This behavior was fixed in Internet Explorer 10.
Opera 10.5 Safari 3 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 11 Safari iOS 1 Samsung Internet Android 1.0
assert Chrome 1 Edge 12 Firefox 28 IE 8 Opera 11 Safari 4 WebView Android 1 Chrome Android 18 Firefox Android 28 Opera Android 11 Safari iOS 3.2 Samsung Internet Android 1.0
clear Chrome Yes Edge 12 Firefox 48 IE Yes Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 48 Opera Android ? Safari iOS ? Samsung Internet Android Yes
count Chrome Yes Edge 12 Firefox 30 IE Yes Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 30 Opera Android ? Safari iOS ? Samsung Internet Android Yes
countReset Chrome Yes Edge ≤79 Firefox 62 IE No Opera Yes Safari ? WebView Android Yes Chrome Android Yes Firefox Android 62 Opera Android ? Safari iOS ? Samsung Internet Android Yes
debug Chrome Yes Edge 12 Firefox 4 IE Yes Opera ? Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 4 Opera Android ? Safari iOS ? Samsung Internet Android Yes
dir
Chrome 1 Edge 12 Firefox 8 IE 9 Opera 11 Safari 4 WebView Android 1 Chrome Android 18 Firefox Android 8 Opera Android 11 Safari iOS 3.2 Samsung Internet Android 1.0
dirxml
Chrome Yes Edge 12 Firefox Yes IE Yes Opera Yes Safari ? WebView Android Yes Chrome Android Yes Firefox Android No Opera Android ? Safari iOS ? Samsung Internet Android Yes
error Chrome 1 Edge 12 Firefox 4 IE 8 Opera 10.5 Safari 3 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 11 Safari iOS 1 Samsung Internet Android 1.0
exception (an alias for error )
弃用 非标
Chrome No Edge 13 — 79 Firefox 28 IE No Opera No Safari No WebView Android No Chrome Android No Firefox Android 28 Opera Android ? Safari iOS No Samsung Internet Android No
group Chrome 1 Edge 12 Firefox 4 IE 11 Opera Yes Safari 4 WebView Android 37 Chrome Android 18 Firefox Android 4 Opera Android ? Safari iOS ? Samsung Internet Android 1.0
groupCollapsed Chrome 6 Edge 12 Firefox 52 IE 11 Opera ? Safari 5.1 WebView Android 37 Chrome Android 18 Firefox Android 52 Opera Android ? Safari iOS ? Samsung Internet Android 1.0
groupEnd Chrome 1 Edge 12 Firefox 9 IE 11 Opera Yes Safari 4 WebView Android 37 Chrome Android 18 Firefox Android 9 Opera Android ? Safari iOS ? Samsung Internet Android 1.0
info Chrome Yes Edge 12 Firefox 4 IE 8 Opera Yes Safari Yes
Yes
No information icon
WebView Android Yes Chrome Android Yes Firefox Android 4 Opera Android ? Safari iOS ? Samsung Internet Android Yes
log Chrome 1 Edge 12 Firefox 4 IE 8 Opera 10.5 Safari 3 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 11 Safari iOS 1 Samsung Internet Android 1.0
profile
非标
Chrome 53 Edge 12 Firefox Yes IE Yes Opera ? Safari ? WebView Android 53 Chrome Android 53 Firefox Android 10 Opera Android ? Safari iOS ? Samsung Internet Android 6.0
profileEnd
非标
Chrome Yes Edge 12 Firefox Yes IE Yes Opera ? Safari ? WebView Android Yes Chrome Android Yes Firefox Android 10 Opera Android ? Safari iOS ? Samsung Internet Android Yes
table Chrome 27 Edge 13 Firefox 34 IE No Opera 11 Safari 6.1 WebView Android ≤37 Chrome Android 27 Firefox Android 34 Opera Android 11 Safari iOS 7 Samsung Internet Android 1.5
time Chrome 1 Edge 12 Firefox 10 IE 11 Opera 11 Safari 4 WebView Android 1 Chrome Android 18 Firefox Android 10 Opera Android 11 Safari iOS 3.2 Samsung Internet Android 1.0
timeEnd Chrome 1 Edge 12 Firefox 10 IE 11 Opera Yes Safari 4 WebView Android 1 Chrome Android 18 Firefox Android 10 Opera Android ? Safari iOS ? Samsung Internet Android 1.0
timeLog Chrome 72 Edge 79 Firefox 62 IE No Opera 60 Safari No
不支持 No
bug 186833 .
WebView Android 72 Chrome Android 72 Firefox Android 62 Opera Android ? Safari iOS No Samsung Internet Android 11.0
timestamp
非标
Chrome Yes Edge 12 Firefox Yes IE 11 Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 10 Opera Android ? Safari iOS ? Samsung Internet Android Yes
trace Chrome 1 Edge 12 Firefox 10 IE 11 Opera 11 Safari 4 WebView Android 1 Chrome Android 18 Firefox Android 10 Opera Android 11 Safari iOS 3.2 Samsung Internet Android 1.0
warn Chrome Yes Edge 12 Firefox 4 IE 8 Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 4 Opera Android ? Safari iOS ? Samsung Internet Android Yes
Available in workers Chrome Yes Edge 12 Firefox 38 IE Yes Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 38 Opera Android ? Safari iOS ? Samsung Internet Android Yes

图例

完整支持
完整支持
不支持
不支持
兼容性未知
兼容性未知
实验。期望将来行为有所改变。
实验。期望将来行为有所改变。
非标。预期跨浏览器支持较差。
非标。预期跨浏览器支持较差。
弃用。不要用于新网站。
弃用。不要用于新网站。
见实现注意事项。

另请参阅