控制台 方法 log() outputs a message to the web console. The message may be a single string (with optional substitution values), or it may be any one or more JavaScript objects.

注意: 此特征可用于 Web 工作者 .

句法

console.log(obj1 [, obj2, ..., objN]);
console.log(msg [, subst1, ..., substN]);
					

参数

obj1 ... objN
A list of JavaScript objects to output. The string representations of each of these objects are appended together in the order listed and output. Please be warned that if you log objects in the latest versions of Chrome and Firefox what you get logged on the console is a reference to the object , which is not necessarily the 'value' of the object at the moment in time you call console.log() , but it is the value of the object at the moment you open the console.
msg

A JavaScript string containing zero or more substitution strings.

subst1 ... substN
JavaScript objects with which to replace substitution strings within msg . This gives you additional control over the format of the output.

把文本输出到控制台 在文档编制的 console 了解细节。

log() 和 dir() 的区别

You might ask yourself what's the difference between console.dir() and console.log() .

Another useful difference in Chrome exists when sending DOM elements to the console.

预告:

  • console.log 以像 HTML 树打印元素
  • console.dir 以像 JSON 树打印元素

具体来说, console.log gives special treatment to DOM elements, whereas console.dir does not. This is often useful when trying to see the full representation of the DOM JS object.

有更多信息在 Chrome 控制台 API 参考 关于此和其它函数。

日志对象

不使用 console.log(obj) ,使用 console.log(JSON.parse(JSON.stringify(obj))) .

This way you are sure you are seeing the value of obj at the moment you log it. Otherwise, many browsers provide a live view that constantly updates as values change. This may not be what you want.

规范

规范 状态 注释
控制台 API
在该规范中的 console.log() 定义。
实时标准 初始定义

浏览器兼容性

The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
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
Substitution strings Chrome Yes
Yes
In version 28, if a negative value is passed to %d, it will be rounded down to the closest negative integer, so -0.1 becomes -1.
Edge 12
12
Before Edge 79, %c 不支持。
Before Edge 79, %d outputs a 0 if the specified value isn't a number.
Firefox 9 IE 10
10
%c 不支持。
%d outputs a 0 if the specified value isn't a number.
Opera ? Safari ? WebView Android Yes
Yes
In version 28, if a negative value is passed to %d, it will be rounded down to the closest negative integer, so -0.1 becomes -1.
Chrome Android Yes
Yes
In version 28, if a negative value is passed to %d, it will be rounded down to the closest negative integer, so -0.1 becomes -1.
Firefox Android 9 Opera Android ? Safari iOS ? Samsung Internet Android Yes
Yes
In Samsung Internet 1.5, if a negative value is passed to %d, it will be rounded down to the closest negative integer, so -0.1 becomes -1.

图例

完整支持

完整支持

兼容性未知 ?

兼容性未知

见实现注意事项。

另请参阅

元数据

  • 最后修改:
  1. 控制台 API
  2. 控制台
  3. 方法
    1. assert()
    2. clear()
    3. count()
    4. countReset()
    5. debug()
    6. dir()
    7. dirxml()
    8. error()
    9. group()
    10. groupCollapsed()
    11. groupEnd()
    12. info()
    13. log()
    14. profile()
    15. profileEnd()
    16. table()
    17. time()
    18. timeEnd()
    19. timeLog()
    20. timeStamp()
    21. trace()
    22. warn()
  4. 控制台 API 相关页面
    1. Window.console
    2. WorkerGlobalScope.console

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1