--- title: Console API slug: Web/API/Console_API translation_of: Web/API/Console_API --- <div>{{DefaultAPISidebar("Console API")}}</div> <p>Console API提供了允许开发人员执行调试任务的功能,例如在代码中的某个位置记录消息或变量值,或者计算任务完成所需的时间。</p> <h2 id="概念和用法"><font><font>概念和用法</font></font></h2> <p>Console API最初是一个专有的API,不同的浏览器以自己的试下方式来实现它. <a href="https://console.spec.whatwg.org/">Console API </a> 规范统一了这个API的行为, 并且所有现代浏览器最终都决定实现这种行为 — 尽管一些实现仍然有自己的附加专有功能. 了解这些请查看:</p> <ul> <li><a href="https://developers.google.com/chrome-developer-tools/docs/console-api">Google Chrome DevTools implementation</a></li> <li><a href="https://developer.apple.com/library/safari/documentation/AppleApplications/Conceptual/Safari_Developer_Guide/Console/Console.html">Safari DevTools implementation</a></li> </ul> <p>用法非常简单 — {{domxref("console")}} 对象 — 可以通过{{domxref("window.console")}}获取到, 在workers里面使用{{domxref("WorkerGlobalScope.console")}}获取,<code>console</code> — 包含许多方法,您可以调用它们来执行基本的调试任务, 通常专注于将各种值记录到浏览器中 <a href="/en-US/docs/Tools/Web_Console">WEB控制台</a>.</p> <p>到目前为止,最常用的方法是console.log,它用于记录特定变量中包含的当前值.</p> <h2 id="接口">接口</h2> <dl> <dt>{{domxref("console")}}</dt> <dd>提供基本的调试功能,包括日志记录,堆栈跟踪,计时器和计数器。</dd> </dl> <h2 id="示例">示例</h2> <pre class="brush: js">let myString = 'Hello world'; // Output "Hello world" to the console console.log(myString)</pre> <p>到<a href="/en-US/docs/Web/API/Console#Usage">Console reference page</a>查看更多示例</p> <h2 id="规范">规范</h2> <table class="standard-table"> <tbody> <tr> <th scope="col">Specification</th> <th scope="col">Status</th> <th scope="col">Comment</th> </tr> <tr> <td>{{SpecName('Console API')}}</td> <td>{{Spec2('Console API')}}</td> <td>Initial definition.</td> </tr> </tbody> </table> <h2 id="浏览器兼容性">浏览器兼容性</h2> <p>{{Compat("api.Console")}}</p> <h2 id="其他">其他</h2> <ul> <li><a href="/en-US/docs/Tools" title="Tools">Tools</a></li> <li><a href="/en-US/docs/Tools/Web_Console" title="Web Console">Web Console</a> — how the Web Console in Firefox handles console API calls</li> <li><a href="/en-US/docs/Tools/Remote_Debugging">Remote debugging</a> — how to see console output when the debugging target is a mobile device</li> </ul>