Console API

Console API提供了允许开发人员执行调试任务的功能,例如在代码中的某个位置记录消息或变量值,或者计算任务完成所需的时间。

概念和用法

Console API最初是一个专有的API,不同的浏览器以自己的试下方式来实现它. Console API  规范统一了这个API的行为, 并且所有现代浏览器最终都决定实现这种行为 — 尽管一些实现仍然有自己的附加专有功能. 了解这些请查看:

用法非常简单 — console 对象 — 可以通过window.console获取到, 在workers里面使用WorkerGlobalScope.console获取,console — 包含许多方法,您可以调用它们来执行基本的调试任务, 通常专注于将各种值记录到浏览器中 WEB控制台.

到目前为止,最常用的方法是console.log,它用于记录特定变量中包含的当前值.

接口

console
提供基本的调试功能,包括日志记录,堆栈跟踪,计时器和计数器。

示例

let myString = 'Hello world';

// Output "Hello world" to the console
console.log(myString)

Console reference page查看更多示例

规范

Specification Status Comment
Console API Living Standard Initial definition.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
ConsoleChrome Full support 1Edge Full support 12Firefox Full support 4IE Full support 8
Notes
Full support 8
Notes
Notes 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 Full support 10.5Safari Full support 3WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 11Safari iOS Full support 1Samsung Internet Android Full support 1.0
assertChrome Full support 1Edge Full support 12Firefox Full support 28IE Full support 8Opera Full support 11Safari Full support 4WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 28Opera Android Full support 11Safari iOS Full support 3.2Samsung Internet Android Full support 1.0
clearChrome Full support 25Edge Full support 12Firefox Full support 39IE Full support 8Opera Full support 12Safari Full support 6.1WebView Android Full support ≤37Chrome Android Full support 25Firefox Android Full support 39Opera Android Full support 12Safari iOS Full support 7Samsung Internet Android Full support 1.5
countChrome Full support 1Edge Full support 12Firefox Full support 30IE Full support 11Opera Full support 11Safari Full support 4WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 30Opera Android Full support 11Safari iOS Full support 3.2Samsung Internet Android Full support 1.0
countResetChrome Full support 68Edge Full support 79Firefox Full support 62IE No support NoOpera Full support 55Safari Full support 13WebView Android Full support 68Chrome Android Full support 68Firefox Android Full support 62Opera Android Full support 48Safari iOS Full support 13Samsung Internet Android Full support 10.0
debugChrome Full support 1Edge Full support 12Firefox Full support 4IE Full support 10Opera Full support 11Safari Full support 4WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 11Safari iOS Full support 3.2Samsung Internet Android Full support 1.0
dir
Experimental
Chrome Full support 1Edge Full support 12Firefox Full support 8IE Full support 9Opera Full support 11Safari Full support 4WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 8Opera Android Full support 11Safari iOS Full support 3.2Samsung Internet Android Full support 1.0
dirxml
Experimental
Chrome Full support 1Edge Full support 12Firefox Full support 39IE Full support 11Opera Full support 11Safari Full support 4WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 39Opera Android Full support 11Safari iOS Full support 3.2Samsung Internet Android Full support 1.0
errorChrome Full support 1Edge Full support 12Firefox Full support 4IE Full support 8Opera Full support 10.5Safari Full support 3WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 11Safari iOS Full support 1Samsung Internet Android Full support 1.0
exception (an alias for error)
DeprecatedNon-standard
Chrome No support NoEdge No support 13 — 79Firefox Full support 28IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoFirefox Android Full support 28Opera Android ? Safari iOS No support NoSamsung Internet Android No support No
groupChrome Full support 1Edge Full support 12Firefox Full support 4IE Full support 11Opera Full support 11Safari Full support 4WebView Android Full support 37Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 11Safari iOS Full support 3.2Samsung Internet Android Full support 1.0
groupCollapsedChrome Full support 6Edge Full support 12Firefox Full support 9IE Full support 11Opera Full support 11Safari Full support 5.1WebView Android Full support 37Chrome Android Full support 18Firefox Android Full support 9Opera Android Full support 11Safari iOS Full support 5.1Samsung Internet Android Full support 1.0
groupEndChrome Full support 1Edge Full support 12Firefox Full support 9IE Full support 11Opera Full support 11Safari Full support 4WebView Android Full support 37Chrome Android Full support 18Firefox Android Full support 9Opera Android Full support 11Safari iOS Full support 3.2Samsung Internet Android Full support 1.0
infoChrome Full support 1Edge Full support 12Firefox Full support 4IE Full support 8Opera Full support 10.5Safari Full support 3WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 11Safari iOS Full support 1Samsung Internet Android Full support 1.0
logChrome Full support 1Edge Full support 12Firefox Full support 4IE Full support 8Opera Full support 10.5Safari Full support 3WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 11Safari iOS Full support 1Samsung Internet Android Full support 1.0
profile
ExperimentalNon-standard
Chrome Full support 4Edge Full support 12Firefox Full support 16IE Full support 9Opera Full support 11Safari Full support 4WebView Android Full support ≤37Chrome Android Full support 18Firefox Android Full support 16Opera Android Full support 11Safari iOS Full support 3.2Samsung Internet Android Full support 1.0
profileEnd
ExperimentalNon-standard
Chrome Full support 4Edge Full support 12Firefox Full support 16IE Full support 9Opera Full support 11Safari Full support 4WebView Android Full support ≤37Chrome Android Full support 18Firefox Android Full support 16Opera Android Full support 11Safari iOS Full support 3.2Samsung Internet Android Full support 1.0
tableChrome Full support 27Edge Full support 13Firefox Full support 34IE No support NoOpera Full support 11Safari Full support 6.1WebView Android Full support ≤37Chrome Android Full support 27Firefox Android Full support 34Opera Android Full support 11Safari iOS Full support 7Samsung Internet Android Full support 1.5
timeChrome Full support 1Edge Full support 12Firefox Full support 10IE Full support 11Opera Full support 11Safari Full support 4WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 10Opera Android Full support 11Safari iOS Full support 3.2Samsung Internet Android Full support 1.0
timeEndChrome Full support 1Edge Full support 12Firefox Full support 10IE Full support 11Opera Full support 11Safari Full support 4WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 10Opera Android Full support 11Safari iOS Full support 3.2Samsung Internet Android Full support 1.0
timeLogChrome Full support 71Edge Full support 79Firefox Full support 62IE No support NoOpera Full support 60Safari Full support 13WebView Android Full support 71Chrome Android Full support 71Firefox Android Full support 62Opera Android Full support 50Safari iOS Full support 13Samsung Internet Android Full support 10.0
timeStamp
ExperimentalNon-standard
Chrome Full support 14Edge Full support 12Firefox Full support 39IE Full support 11Opera Full support 15Safari Full support 6WebView Android Full support ≤37Chrome Android Full support 18Firefox Android Full support 39Opera Android Full support 14Safari iOS Full support 6Samsung Internet Android Full support 1.0
traceChrome Full support 1Edge Full support 12Firefox Full support 10IE Full support 11Opera Full support 11Safari Full support 4WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 10Opera Android Full support 11Safari iOS Full support 3.2Samsung Internet Android Full support 1.0
warnChrome Full support 1Edge Full support 12Firefox Full support 4IE Full support 8Opera Full support 10.5Safari Full support 3WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 11Safari iOS Full support 1Samsung Internet Android Full support 1.0
Available in workersChrome Full support YesEdge Full support 12Firefox Full support 38IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 38Opera Android ? Safari iOS ? Samsung Internet Android Full support Yes

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.
Deprecated. Not for use in new websites.
Deprecated. Not for use in new websites.
See implementation notes.
See implementation notes.

其他

  • Tools
  • Web Console — how the Web Console in Firefox handles console API calls
  • Remote debugging — how to see console output when the debugging target is a mobile device