Console API

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.

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

概念和用法

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

用法非常简单——console 对象包含许多方法,你可以调用它们来执行基本的调试任务,通常专注于将各种值记录到浏览器的 Web 控制台

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

接口

console

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

示例

js
let myString = "你好世界";

// 在控制台中输出“你好世界”
console.log(myString);

参见 console 参考页面以获取更多示例。

规范

Specification
Console
# console-namespace

浏览器兼容性

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
console
assert() static method
clear() static method
countReset() static method
count() static method
debug() static method
dir() static method
options.colors parameter
ExperimentalNon-standard
options.depth parameter
ExperimentalNon-standard
options.showHidden parameter
ExperimentalNon-standard
dirxml() static method
error() static method
exception() static method (an alias for error())
DeprecatedNon-standard
groupCollapsed() static method
groupEnd() static method
group() static method
info() static method
log() static method
profileEnd() static method
Non-standard
profile() static method
Non-standard
Substitution strings
table() static method
timeEnd() static method
timeLog() static method
timeStamp() static method
Non-standard
time() static method
trace() static method
warn() static method
Available in workers

Legend

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

Full support
Full support
Partial support
Partial support
No support
No support
Experimental. Expect behavior to change in the future.
Non-standard. Check cross-browser support before using.
Deprecated. Not for use in new websites.
See implementation notes.
User must explicitly enable this feature.
Has more compatibility info.

参见

  • 工具
  • Web 控制台——Firefox 中的 Web 控制台是如何处理 console API 调用的
  • about:debugging——在调试目标为移动设备时如何查看控制台输出