• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html>
2<html lang="en">
3<head>
4  <meta charset="utf-8">
5  <meta name="viewport" content="width=device-width">
6  <meta name="nodejs.org:node-version" content="v18.18.2">
7  <title>Console | Node.js v18.18.2 Documentation</title>
8  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
9  <link rel="stylesheet" href="assets/style.css">
10  <link rel="stylesheet" href="assets/hljs.css">
11  <link rel="canonical" href="https://nodejs.org/api/console.html">
12  <script async defer src="assets/api.js" type="text/javascript"></script>
13
14</head>
15<body class="alt apidoc" id="api-section-console">
16  <div id="content" class="clearfix">
17    <div id="column2" class="interior">
18      <div id="intro" class="interior">
19        <a href="/" title="Go back to the home page">
20          Node.js
21        </a>
22      </div>
23      <ul>
24<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
25<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
26</ul>
27<hr class="line">
28<ul>
29<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
30<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
31<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
32<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
33<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
34<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
35<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
36<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
37<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
38<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
39<li><a href="console.html" class="nav-console active">Console</a></li>
40<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
41<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
42<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
43<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
44<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
45<li><a href="dns.html" class="nav-dns">DNS</a></li>
46<li><a href="domain.html" class="nav-domain">Domain</a></li>
47<li><a href="errors.html" class="nav-errors">Errors</a></li>
48<li><a href="events.html" class="nav-events">Events</a></li>
49<li><a href="fs.html" class="nav-fs">File system</a></li>
50<li><a href="globals.html" class="nav-globals">Globals</a></li>
51<li><a href="http.html" class="nav-http">HTTP</a></li>
52<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
53<li><a href="https.html" class="nav-https">HTTPS</a></li>
54<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
55<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
56<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
57<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
58<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
59<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
60<li><a href="net.html" class="nav-net">Net</a></li>
61<li><a href="os.html" class="nav-os">OS</a></li>
62<li><a href="path.html" class="nav-path">Path</a></li>
63<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
64<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
65<li><a href="process.html" class="nav-process">Process</a></li>
66<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
67<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
68<li><a href="readline.html" class="nav-readline">Readline</a></li>
69<li><a href="repl.html" class="nav-repl">REPL</a></li>
70<li><a href="report.html" class="nav-report">Report</a></li>
71<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
72<li><a href="stream.html" class="nav-stream">Stream</a></li>
73<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
74<li><a href="test.html" class="nav-test">Test runner</a></li>
75<li><a href="timers.html" class="nav-timers">Timers</a></li>
76<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
77<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
78<li><a href="tty.html" class="nav-tty">TTY</a></li>
79<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
80<li><a href="url.html" class="nav-url">URL</a></li>
81<li><a href="util.html" class="nav-util">Utilities</a></li>
82<li><a href="v8.html" class="nav-v8">V8</a></li>
83<li><a href="vm.html" class="nav-vm">VM</a></li>
84<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
85<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
86<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
87<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
88<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
89</ul>
90<hr class="line">
91<ul>
92<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
93</ul>
94    </div>
95
96    <div id="column1" data-id="console" class="interior">
97      <header class="header">
98        <div class="header-container">
99          <h1>Node.js v18.18.2 documentation</h1>
100          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
101            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
102              <path fill="none" d="M0 0h24v24H0z" />
103              <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/>
104              <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/>
105            </svg>
106            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
107              <path d="M0 0h24v24H0z" fill="none" />
108              <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/>
109            </svg>
110          </button>
111        </div>
112        <div id="gtoc">
113          <ul>
114            <li class="pinned-header">Node.js v18.18.2</li>
115
116    <li class="picker-header">
117      <a href="#">
118        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
119        Table of contents
120      </a>
121
122      <div class="picker"><div class="toc"><ul>
123<li><span class="stability_2"><a href="#console">Console</a></span>
124<ul>
125<li><a href="#class-console">Class: <code>Console</code></a>
126<ul>
127<li><a href="#new-consolestdout-stderr-ignoreerrors"><code>new Console(stdout[, stderr][, ignoreErrors])</code></a></li>
128<li><a href="#new-consoleoptions"><code>new Console(options)</code></a></li>
129<li><a href="#consoleassertvalue-message"><code>console.assert(value[, ...message])</code></a></li>
130<li><a href="#consoleclear"><code>console.clear()</code></a></li>
131<li><a href="#consolecountlabel"><code>console.count([label])</code></a></li>
132<li><a href="#consolecountresetlabel"><code>console.countReset([label])</code></a></li>
133<li><a href="#consoledebugdata-args"><code>console.debug(data[, ...args])</code></a></li>
134<li><a href="#consoledirobj-options"><code>console.dir(obj[, options])</code></a></li>
135<li><a href="#consoledirxmldata"><code>console.dirxml(...data)</code></a></li>
136<li><a href="#consoleerrordata-args"><code>console.error([data][, ...args])</code></a></li>
137<li><a href="#consolegrouplabel"><code>console.group([...label])</code></a></li>
138<li><a href="#consolegroupcollapsed"><code>console.groupCollapsed()</code></a></li>
139<li><a href="#consolegroupend"><code>console.groupEnd()</code></a></li>
140<li><a href="#consoleinfodata-args"><code>console.info([data][, ...args])</code></a></li>
141<li><a href="#consolelogdata-args"><code>console.log([data][, ...args])</code></a></li>
142<li><a href="#consoletabletabulardata-properties"><code>console.table(tabularData[, properties])</code></a></li>
143<li><a href="#consoletimelabel"><code>console.time([label])</code></a></li>
144<li><a href="#consoletimeendlabel"><code>console.timeEnd([label])</code></a></li>
145<li><a href="#consoletimeloglabel-data"><code>console.timeLog([label][, ...data])</code></a></li>
146<li><a href="#consoletracemessage-args"><code>console.trace([message][, ...args])</code></a></li>
147<li><a href="#consolewarndata-args"><code>console.warn([data][, ...args])</code></a></li>
148</ul>
149</li>
150<li><a href="#inspector-only-methods">Inspector only methods</a>
151<ul>
152<li><a href="#consoleprofilelabel"><code>console.profile([label])</code></a></li>
153<li><a href="#consoleprofileendlabel"><code>console.profileEnd([label])</code></a></li>
154<li><a href="#consoletimestamplabel"><code>console.timeStamp([label])</code></a></li>
155</ul>
156</li>
157</ul>
158</li>
159</ul></div></div>
160    </li>
161
162
163    <li class="picker-header">
164      <a href="#">
165        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
166        Index
167      </a>
168
169      <div class="picker"><ul>
170<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
171<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
172
173      <li>
174        <a href="index.html">Index</a>
175      </li>
176    </ul>
177
178<hr class="line">
179<ul>
180<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
181<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
182<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
183<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
184<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
185<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
186<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
187<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
188<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
189<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
190<li><a href="console.html" class="nav-console active">Console</a></li>
191<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
192<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
193<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
194<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
195<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
196<li><a href="dns.html" class="nav-dns">DNS</a></li>
197<li><a href="domain.html" class="nav-domain">Domain</a></li>
198<li><a href="errors.html" class="nav-errors">Errors</a></li>
199<li><a href="events.html" class="nav-events">Events</a></li>
200<li><a href="fs.html" class="nav-fs">File system</a></li>
201<li><a href="globals.html" class="nav-globals">Globals</a></li>
202<li><a href="http.html" class="nav-http">HTTP</a></li>
203<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
204<li><a href="https.html" class="nav-https">HTTPS</a></li>
205<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
206<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
207<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
208<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
209<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
210<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
211<li><a href="net.html" class="nav-net">Net</a></li>
212<li><a href="os.html" class="nav-os">OS</a></li>
213<li><a href="path.html" class="nav-path">Path</a></li>
214<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
215<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
216<li><a href="process.html" class="nav-process">Process</a></li>
217<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
218<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
219<li><a href="readline.html" class="nav-readline">Readline</a></li>
220<li><a href="repl.html" class="nav-repl">REPL</a></li>
221<li><a href="report.html" class="nav-report">Report</a></li>
222<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
223<li><a href="stream.html" class="nav-stream">Stream</a></li>
224<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
225<li><a href="test.html" class="nav-test">Test runner</a></li>
226<li><a href="timers.html" class="nav-timers">Timers</a></li>
227<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
228<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
229<li><a href="tty.html" class="nav-tty">TTY</a></li>
230<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
231<li><a href="url.html" class="nav-url">URL</a></li>
232<li><a href="util.html" class="nav-util">Utilities</a></li>
233<li><a href="v8.html" class="nav-v8">V8</a></li>
234<li><a href="vm.html" class="nav-vm">VM</a></li>
235<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
236<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
237<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
238<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
239<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
240</ul>
241<hr class="line">
242<ul>
243<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
244</ul></div>
245    </li>
246
247
248    <li class="picker-header">
249      <a href="#">
250        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
251        Other versions
252      </a>
253      <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v20.x/api/console.html">20.x</a></li>
254<li><a href="https://nodejs.org/docs/latest-v19.x/api/console.html">19.x</a></li>
255<li><a href="https://nodejs.org/docs/latest-v18.x/api/console.html">18.x <b>LTS</b></a></li>
256<li><a href="https://nodejs.org/docs/latest-v17.x/api/console.html">17.x</a></li>
257<li><a href="https://nodejs.org/docs/latest-v16.x/api/console.html">16.x</a></li>
258<li><a href="https://nodejs.org/docs/latest-v15.x/api/console.html">15.x</a></li>
259<li><a href="https://nodejs.org/docs/latest-v14.x/api/console.html">14.x</a></li>
260<li><a href="https://nodejs.org/docs/latest-v13.x/api/console.html">13.x</a></li>
261<li><a href="https://nodejs.org/docs/latest-v12.x/api/console.html">12.x</a></li>
262<li><a href="https://nodejs.org/docs/latest-v11.x/api/console.html">11.x</a></li>
263<li><a href="https://nodejs.org/docs/latest-v10.x/api/console.html">10.x</a></li>
264<li><a href="https://nodejs.org/docs/latest-v9.x/api/console.html">9.x</a></li>
265<li><a href="https://nodejs.org/docs/latest-v8.x/api/console.html">8.x</a></li>
266<li><a href="https://nodejs.org/docs/latest-v7.x/api/console.html">7.x</a></li>
267<li><a href="https://nodejs.org/docs/latest-v6.x/api/console.html">6.x</a></li>
268<li><a href="https://nodejs.org/docs/latest-v5.x/api/console.html">5.x</a></li>
269<li><a href="https://nodejs.org/docs/latest-v4.x/api/console.html">4.x</a></li>
270<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/console.html">0.12.x</a></li>
271<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/console.html">0.10.x</a></li></ol></div>
272    </li>
273
274            <li class="picker-header">
275              <a href="#">
276                <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
277                Options
278              </a>
279
280              <div class="picker">
281                <ul>
282                  <li>
283                    <a href="all.html">View on single page</a>
284                  </li>
285                  <li>
286                    <a href="console.json">View as JSON</a>
287                  </li>
288                  <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/console.md">Edit on GitHub</a></li>
289                </ul>
290              </div>
291            </li>
292          </ul>
293        </div>
294        <hr>
295      </header>
296
297      <details id="toc" open><summary>Table of contents</summary><ul>
298<li><span class="stability_2"><a href="#console">Console</a></span>
299<ul>
300<li><a href="#class-console">Class: <code>Console</code></a>
301<ul>
302<li><a href="#new-consolestdout-stderr-ignoreerrors"><code>new Console(stdout[, stderr][, ignoreErrors])</code></a></li>
303<li><a href="#new-consoleoptions"><code>new Console(options)</code></a></li>
304<li><a href="#consoleassertvalue-message"><code>console.assert(value[, ...message])</code></a></li>
305<li><a href="#consoleclear"><code>console.clear()</code></a></li>
306<li><a href="#consolecountlabel"><code>console.count([label])</code></a></li>
307<li><a href="#consolecountresetlabel"><code>console.countReset([label])</code></a></li>
308<li><a href="#consoledebugdata-args"><code>console.debug(data[, ...args])</code></a></li>
309<li><a href="#consoledirobj-options"><code>console.dir(obj[, options])</code></a></li>
310<li><a href="#consoledirxmldata"><code>console.dirxml(...data)</code></a></li>
311<li><a href="#consoleerrordata-args"><code>console.error([data][, ...args])</code></a></li>
312<li><a href="#consolegrouplabel"><code>console.group([...label])</code></a></li>
313<li><a href="#consolegroupcollapsed"><code>console.groupCollapsed()</code></a></li>
314<li><a href="#consolegroupend"><code>console.groupEnd()</code></a></li>
315<li><a href="#consoleinfodata-args"><code>console.info([data][, ...args])</code></a></li>
316<li><a href="#consolelogdata-args"><code>console.log([data][, ...args])</code></a></li>
317<li><a href="#consoletabletabulardata-properties"><code>console.table(tabularData[, properties])</code></a></li>
318<li><a href="#consoletimelabel"><code>console.time([label])</code></a></li>
319<li><a href="#consoletimeendlabel"><code>console.timeEnd([label])</code></a></li>
320<li><a href="#consoletimeloglabel-data"><code>console.timeLog([label][, ...data])</code></a></li>
321<li><a href="#consoletracemessage-args"><code>console.trace([message][, ...args])</code></a></li>
322<li><a href="#consolewarndata-args"><code>console.warn([data][, ...args])</code></a></li>
323</ul>
324</li>
325<li><a href="#inspector-only-methods">Inspector only methods</a>
326<ul>
327<li><a href="#consoleprofilelabel"><code>console.profile([label])</code></a></li>
328<li><a href="#consoleprofileendlabel"><code>console.profileEnd([label])</code></a></li>
329<li><a href="#consoletimestamplabel"><code>console.timeStamp([label])</code></a></li>
330</ul>
331</li>
332</ul>
333</li>
334</ul></details>
335
336      <div id="apicontent">
337        <h2>Console<span><a class="mark" href="#console" id="console">#</a></span><a aria-hidden="true" class="legacy" id="console_console"></a></h2>
338
339<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p>
340<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.18.2/lib/console.js">lib/console.js</a></p>
341<p>The <code>node:console</code> module provides a simple debugging console that is similar to
342the JavaScript console mechanism provided by web browsers.</p>
343<p>The module exports two specific components:</p>
344<ul>
345<li>A <code>Console</code> class with methods such as <code>console.log()</code>, <code>console.error()</code>, and
346<code>console.warn()</code> that can be used to write to any Node.js stream.</li>
347<li>A global <code>console</code> instance configured to write to <a href="process.html#processstdout"><code>process.stdout</code></a> and
348<a href="process.html#processstderr"><code>process.stderr</code></a>. The global <code>console</code> can be used without calling
349<code>require('node:console')</code>.</li>
350</ul>
351<p><em><strong>Warning</strong></em>: The global console object's methods are neither consistently
352synchronous like the browser APIs they resemble, nor are they consistently
353asynchronous like all other Node.js streams. See the <a href="process.html#a-note-on-process-io">note on process I/O</a> for
354more information.</p>
355<p>Example using the global <code>console</code>:</p>
356<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'hello world'</span>);
357<span class="hljs-comment">// Prints: hello world, to stdout</span>
358<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'hello %s'</span>, <span class="hljs-string">'world'</span>);
359<span class="hljs-comment">// Prints: hello world, to stdout</span>
360<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'Whoops, something bad happened'</span>));
361<span class="hljs-comment">// Prints error message and stack trace to stderr:</span>
362<span class="hljs-comment">//   Error: Whoops, something bad happened</span>
363<span class="hljs-comment">//     at [eval]:5:15</span>
364<span class="hljs-comment">//     at Script.runInThisContext (node:vm:132:18)</span>
365<span class="hljs-comment">//     at Object.runInThisContext (node:vm:309:38)</span>
366<span class="hljs-comment">//     at node:internal/process/execution:77:19</span>
367<span class="hljs-comment">//     at [eval]-wrapper:6:22</span>
368<span class="hljs-comment">//     at evalScript (node:internal/process/execution:76:60)</span>
369<span class="hljs-comment">//     at node:internal/main/eval_string:23:3</span>
370
371<span class="hljs-keyword">const</span> name = <span class="hljs-string">'Will Robinson'</span>;
372<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">warn</span>(<span class="hljs-string">`Danger <span class="hljs-subst">${name}</span>! Danger!`</span>);
373<span class="hljs-comment">// Prints: Danger Will Robinson! Danger!, to stderr</span></code> <button class="copy-button">copy</button></pre>
374<p>Example using the <code>Console</code> class:</p>
375<pre><code class="language-js"><span class="hljs-keyword">const</span> out = <span class="hljs-title function_">getStreamSomehow</span>();
376<span class="hljs-keyword">const</span> err = <span class="hljs-title function_">getStreamSomehow</span>();
377<span class="hljs-keyword">const</span> myConsole = <span class="hljs-keyword">new</span> <span class="hljs-variable language_">console</span>.<span class="hljs-title class_">Console</span>(out, err);
378
379myConsole.<span class="hljs-title function_">log</span>(<span class="hljs-string">'hello world'</span>);
380<span class="hljs-comment">// Prints: hello world, to out</span>
381myConsole.<span class="hljs-title function_">log</span>(<span class="hljs-string">'hello %s'</span>, <span class="hljs-string">'world'</span>);
382<span class="hljs-comment">// Prints: hello world, to out</span>
383myConsole.<span class="hljs-title function_">error</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'Whoops, something bad happened'</span>));
384<span class="hljs-comment">// Prints: [Error: Whoops, something bad happened], to err</span>
385
386<span class="hljs-keyword">const</span> name = <span class="hljs-string">'Will Robinson'</span>;
387myConsole.<span class="hljs-title function_">warn</span>(<span class="hljs-string">`Danger <span class="hljs-subst">${name}</span>! Danger!`</span>);
388<span class="hljs-comment">// Prints: Danger Will Robinson! Danger!, to err</span></code> <button class="copy-button">copy</button></pre>
389<section><h3>Class: <code>Console</code><span><a class="mark" href="#class-console" id="class-console">#</a></span><a aria-hidden="true" class="legacy" id="console_class_console"></a></h3>
390<div class="api_metadata">
391<details class="changelog"><summary>History</summary>
392<table>
393<tbody><tr><th>Version</th><th>Changes</th></tr>
394<tr><td>v8.0.0</td>
395<td><p>Errors that occur while writing to the underlying streams will now be ignored by default.</p></td></tr>
396</tbody></table>
397</details>
398</div>
399
400<p>The <code>Console</code> class can be used to create a simple logger with configurable
401output streams and can be accessed using either <code>require('node:console').Console</code>
402or <code>console.Console</code> (or their destructured counterparts):</p>
403<pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Console</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:console'</span>);</code> <button class="copy-button">copy</button></pre>
404<pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Console</span> } = <span class="hljs-variable language_">console</span>;</code> <button class="copy-button">copy</button></pre>
405<h4><code>new Console(stdout[, stderr][, ignoreErrors])</code><span><a class="mark" href="#new-consolestdout-stderr-ignoreerrors" id="new-consolestdout-stderr-ignoreerrors">#</a></span><a aria-hidden="true" class="legacy" id="console_new_console_stdout_stderr_ignoreerrors"></a></h4>
406<h4><code>new Console(options)</code><span><a class="mark" href="#new-consoleoptions" id="new-consoleoptions">#</a></span><a aria-hidden="true" class="legacy" id="console_new_console_options"></a></h4>
407<div class="api_metadata">
408<details class="changelog"><summary>History</summary>
409<table>
410<tbody><tr><th>Version</th><th>Changes</th></tr>
411<tr><td>v14.2.0, v12.17.0</td>
412<td><p>The <code>groupIndentation</code> option was introduced.</p></td></tr>
413<tr><td>v11.7.0</td>
414<td><p>The <code>inspectOptions</code> option is introduced.</p></td></tr>
415<tr><td>v10.0.0</td>
416<td><p>The <code>Console</code> constructor now supports an <code>options</code> argument, and the <code>colorMode</code> option was introduced.</p></td></tr>
417<tr><td>v8.0.0</td>
418<td><p>The <code>ignoreErrors</code> option was introduced.</p></td></tr>
419</tbody></table>
420</details>
421</div>
422<ul>
423<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
424<ul>
425<li><code>stdout</code> <a href="stream.html#class-streamwritable" class="type">&#x3C;stream.Writable></a></li>
426<li><code>stderr</code> <a href="stream.html#class-streamwritable" class="type">&#x3C;stream.Writable></a></li>
427<li><code>ignoreErrors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Ignore errors when writing to the underlying
428streams. <strong>Default:</strong> <code>true</code>.</li>
429<li><code>colorMode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Set color support for this <code>Console</code> instance.
430Setting to <code>true</code> enables coloring while inspecting values. Setting to
431<code>false</code> disables coloring while inspecting values. Setting to
432<code>'auto'</code> makes color support depend on the value of the <code>isTTY</code> property
433and the value returned by <code>getColorDepth()</code> on the respective stream. This
434option can not be used, if <code>inspectOptions.colors</code> is set as well.
435<strong>Default:</strong> <code>'auto'</code>.</li>
436<li><code>inspectOptions</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> Specifies options that are passed along to
437<a href="util.html#utilinspectobject-options"><code>util.inspect()</code></a>.</li>
438<li><code>groupIndentation</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Set group indentation.
439<strong>Default:</strong> <code>2</code>.</li>
440</ul>
441</li>
442</ul>
443<p>Creates a new <code>Console</code> with one or two writable stream instances. <code>stdout</code> is a
444writable stream to print log or info output. <code>stderr</code> is used for warning or
445error output. If <code>stderr</code> is not provided, <code>stdout</code> is used for <code>stderr</code>.</p>
446<pre><code class="language-js"><span class="hljs-keyword">const</span> output = fs.<span class="hljs-title function_">createWriteStream</span>(<span class="hljs-string">'./stdout.log'</span>);
447<span class="hljs-keyword">const</span> errorOutput = fs.<span class="hljs-title function_">createWriteStream</span>(<span class="hljs-string">'./stderr.log'</span>);
448<span class="hljs-comment">// Custom simple logger</span>
449<span class="hljs-keyword">const</span> logger = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Console</span>({ <span class="hljs-attr">stdout</span>: output, <span class="hljs-attr">stderr</span>: errorOutput });
450<span class="hljs-comment">// use it like console</span>
451<span class="hljs-keyword">const</span> count = <span class="hljs-number">5</span>;
452logger.<span class="hljs-title function_">log</span>(<span class="hljs-string">'count: %d'</span>, count);
453<span class="hljs-comment">// In stdout.log: count 5</span></code> <button class="copy-button">copy</button></pre>
454<p>The global <code>console</code> is a special <code>Console</code> whose output is sent to
455<a href="process.html#processstdout"><code>process.stdout</code></a> and <a href="process.html#processstderr"><code>process.stderr</code></a>. It is equivalent to calling:</p>
456<pre><code class="language-js"><span class="hljs-keyword">new</span> <span class="hljs-title class_">Console</span>({ <span class="hljs-attr">stdout</span>: process.<span class="hljs-property">stdout</span>, <span class="hljs-attr">stderr</span>: process.<span class="hljs-property">stderr</span> });</code> <button class="copy-button">copy</button></pre>
457<h4><code>console.assert(value[, ...message])</code><span><a class="mark" href="#consoleassertvalue-message" id="consoleassertvalue-message">#</a></span><a aria-hidden="true" class="legacy" id="console_console_assert_value_message"></a></h4>
458<div class="api_metadata">
459<details class="changelog"><summary>History</summary>
460<table>
461<tbody><tr><th>Version</th><th>Changes</th></tr>
462<tr><td>v10.0.0</td>
463<td><p>The implementation is now spec compliant and does not throw anymore.</p></td></tr>
464<tr><td>v0.1.101</td>
465<td><p><span>Added in: v0.1.101</span></p></td></tr>
466</tbody></table>
467</details>
468</div>
469<ul>
470<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> The value tested for being truthy.</li>
471<li><code>...message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> All arguments besides <code>value</code> are used as error message.</li>
472</ul>
473<p><code>console.assert()</code> writes a message if <code>value</code> is <a href="https://developer.mozilla.org/en-US/docs/Glossary/Falsy">falsy</a> or omitted. It only
474writes a message and does not otherwise affect execution. The output always
475starts with <code>"Assertion failed"</code>. If provided, <code>message</code> is formatted using
476<a href="util.html#utilformatformat-args"><code>util.format()</code></a>.</p>
477<p>If <code>value</code> is <a href="https://developer.mozilla.org/en-US/docs/Glossary/Truthy">truthy</a>, nothing happens.</p>
478<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">assert</span>(<span class="hljs-literal">true</span>, <span class="hljs-string">'does nothing'</span>);
479
480<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">assert</span>(<span class="hljs-literal">false</span>, <span class="hljs-string">'Whoops %s work'</span>, <span class="hljs-string">'didn\'t'</span>);
481<span class="hljs-comment">// Assertion failed: Whoops didn't work</span>
482
483<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">assert</span>();
484<span class="hljs-comment">// Assertion failed</span></code> <button class="copy-button">copy</button></pre>
485<h4><code>console.clear()</code><span><a class="mark" href="#consoleclear" id="consoleclear">#</a></span><a aria-hidden="true" class="legacy" id="console_console_clear"></a></h4>
486<div class="api_metadata">
487<span>Added in: v8.3.0</span>
488</div>
489<p>When <code>stdout</code> is a TTY, calling <code>console.clear()</code> will attempt to clear the
490TTY. When <code>stdout</code> is not a TTY, this method does nothing.</p>
491<p>The specific operation of <code>console.clear()</code> can vary across operating systems
492and terminal types. For most Linux operating systems, <code>console.clear()</code>
493operates similarly to the <code>clear</code> shell command. On Windows, <code>console.clear()</code>
494will clear only the output in the current terminal viewport for the Node.js
495binary.</p>
496<h4><code>console.count([label])</code><span><a class="mark" href="#consolecountlabel" id="consolecountlabel">#</a></span><a aria-hidden="true" class="legacy" id="console_console_count_label"></a></h4>
497<div class="api_metadata">
498<span>Added in: v8.3.0</span>
499</div>
500<ul>
501<li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The display label for the counter. <strong>Default:</strong> <code>'default'</code>.</li>
502</ul>
503<p>Maintains an internal counter specific to <code>label</code> and outputs to <code>stdout</code> the
504number of times <code>console.count()</code> has been called with the given <code>label</code>.</p>
505<!-- eslint-skip -->
506<pre><code class="language-js">> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">count</span>()
507<span class="hljs-attr">default</span>: <span class="hljs-number">1</span>
508<span class="hljs-literal">undefined</span>
509> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">count</span>(<span class="hljs-string">'default'</span>)
510<span class="hljs-attr">default</span>: <span class="hljs-number">2</span>
511<span class="hljs-literal">undefined</span>
512> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">count</span>(<span class="hljs-string">'abc'</span>)
513<span class="hljs-attr">abc</span>: <span class="hljs-number">1</span>
514<span class="hljs-literal">undefined</span>
515> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">count</span>(<span class="hljs-string">'xyz'</span>)
516<span class="hljs-attr">xyz</span>: <span class="hljs-number">1</span>
517<span class="hljs-literal">undefined</span>
518> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">count</span>(<span class="hljs-string">'abc'</span>)
519<span class="hljs-attr">abc</span>: <span class="hljs-number">2</span>
520<span class="hljs-literal">undefined</span>
521> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">count</span>()
522<span class="hljs-attr">default</span>: <span class="hljs-number">3</span>
523<span class="hljs-literal">undefined</span>
524></code> <button class="copy-button">copy</button></pre>
525<h4><code>console.countReset([label])</code><span><a class="mark" href="#consolecountresetlabel" id="consolecountresetlabel">#</a></span><a aria-hidden="true" class="legacy" id="console_console_countreset_label"></a></h4>
526<div class="api_metadata">
527<span>Added in: v8.3.0</span>
528</div>
529<ul>
530<li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The display label for the counter. <strong>Default:</strong> <code>'default'</code>.</li>
531</ul>
532<p>Resets the internal counter specific to <code>label</code>.</p>
533<!-- eslint-skip -->
534<pre><code class="language-js">> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">count</span>(<span class="hljs-string">'abc'</span>);
535<span class="hljs-attr">abc</span>: <span class="hljs-number">1</span>
536<span class="hljs-literal">undefined</span>
537> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">countReset</span>(<span class="hljs-string">'abc'</span>);
538<span class="hljs-literal">undefined</span>
539> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">count</span>(<span class="hljs-string">'abc'</span>);
540<span class="hljs-attr">abc</span>: <span class="hljs-number">1</span>
541<span class="hljs-literal">undefined</span>
542></code> <button class="copy-button">copy</button></pre>
543<h4><code>console.debug(data[, ...args])</code><span><a class="mark" href="#consoledebugdata-args" id="consoledebugdata-args">#</a></span><a aria-hidden="true" class="legacy" id="console_console_debug_data_args"></a></h4>
544<div class="api_metadata">
545<details class="changelog"><summary>History</summary>
546<table>
547<tbody><tr><th>Version</th><th>Changes</th></tr>
548<tr><td>v8.10.0</td>
549<td><p><code>console.debug</code> is now an alias for <code>console.log</code>.</p></td></tr>
550<tr><td>v8.0.0</td>
551<td><p><span>Added in: v8.0.0</span></p></td></tr>
552</tbody></table>
553</details>
554</div>
555<ul>
556<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
557<li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
558</ul>
559<p>The <code>console.debug()</code> function is an alias for <a href="#consolelogdata-args"><code>console.log()</code></a>.</p>
560<h4><code>console.dir(obj[, options])</code><span><a class="mark" href="#consoledirobj-options" id="consoledirobj-options">#</a></span><a aria-hidden="true" class="legacy" id="console_console_dir_obj_options"></a></h4>
561<div class="api_metadata">
562<span>Added in: v0.1.101</span>
563</div>
564<ul>
565<li><code>obj</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
566<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
567<ul>
568<li><code>showHidden</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If <code>true</code> then the object's non-enumerable and symbol
569properties will be shown too. <strong>Default:</strong> <code>false</code>.</li>
570<li><code>depth</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Tells <a href="util.html#utilinspectobject-options"><code>util.inspect()</code></a> how many times to recurse while
571formatting the object. This is useful for inspecting large complicated
572objects. To make it recurse indefinitely, pass <code>null</code>. <strong>Default:</strong> <code>2</code>.</li>
573<li><code>colors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If <code>true</code>, then the output will be styled with ANSI color
574codes. Colors are customizable;
575see <a href="util.html#customizing-utilinspect-colors">customizing <code>util.inspect()</code> colors</a>. <strong>Default:</strong> <code>false</code>.</li>
576</ul>
577</li>
578</ul>
579<p>Uses <a href="util.html#utilinspectobject-options"><code>util.inspect()</code></a> on <code>obj</code> and prints the resulting string to <code>stdout</code>.
580This function bypasses any custom <code>inspect()</code> function defined on <code>obj</code>.</p>
581<h4><code>console.dirxml(...data)</code><span><a class="mark" href="#consoledirxmldata" id="consoledirxmldata">#</a></span><a aria-hidden="true" class="legacy" id="console_console_dirxml_data"></a></h4>
582<div class="api_metadata">
583<details class="changelog"><summary>History</summary>
584<table>
585<tbody><tr><th>Version</th><th>Changes</th></tr>
586<tr><td>v9.3.0</td>
587<td><p><code>console.dirxml</code> now calls <code>console.log</code> for its arguments.</p></td></tr>
588<tr><td>v8.0.0</td>
589<td><p><span>Added in: v8.0.0</span></p></td></tr>
590</tbody></table>
591</details>
592</div>
593<ul>
594<li><code>...data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
595</ul>
596<p>This method calls <code>console.log()</code> passing it the arguments received.
597This method does not produce any XML formatting.</p>
598<h4><code>console.error([data][, ...args])</code><span><a class="mark" href="#consoleerrordata-args" id="consoleerrordata-args">#</a></span><a aria-hidden="true" class="legacy" id="console_console_error_data_args"></a></h4>
599<div class="api_metadata">
600<span>Added in: v0.1.100</span>
601</div>
602<ul>
603<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
604<li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
605</ul>
606<p>Prints to <code>stderr</code> with newline. Multiple arguments can be passed, with the
607first used as the primary message and all additional used as substitution
608values similar to <a href="http://man7.org/linux/man-pages/man3/printf.3.html"><code>printf(3)</code></a> (the arguments are all passed to
609<a href="util.html#utilformatformat-args"><code>util.format()</code></a>).</p>
610<pre><code class="language-js"><span class="hljs-keyword">const</span> code = <span class="hljs-number">5</span>;
611<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'error #%d'</span>, code);
612<span class="hljs-comment">// Prints: error #5, to stderr</span>
613<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'error'</span>, code);
614<span class="hljs-comment">// Prints: error 5, to stderr</span></code> <button class="copy-button">copy</button></pre>
615<p>If formatting elements (e.g. <code>%d</code>) are not found in the first string then
616<a href="util.html#utilinspectobject-options"><code>util.inspect()</code></a> is called on each argument and the resulting string
617values are concatenated. See <a href="util.html#utilformatformat-args"><code>util.format()</code></a> for more information.</p>
618<h4><code>console.group([...label])</code><span><a class="mark" href="#consolegrouplabel" id="consolegrouplabel">#</a></span><a aria-hidden="true" class="legacy" id="console_console_group_label"></a></h4>
619<div class="api_metadata">
620<span>Added in: v8.5.0</span>
621</div>
622<ul>
623<li><code>...label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
624</ul>
625<p>Increases indentation of subsequent lines by spaces for <code>groupIndentation</code>
626length.</p>
627<p>If one or more <code>label</code>s are provided, those are printed first without the
628additional indentation.</p>
629<h4><code>console.groupCollapsed()</code><span><a class="mark" href="#consolegroupcollapsed" id="consolegroupcollapsed">#</a></span><a aria-hidden="true" class="legacy" id="console_console_groupcollapsed"></a></h4>
630<div class="api_metadata">
631<span>Added in: v8.5.0</span>
632</div>
633<p>An alias for <a href="#consolegrouplabel"><code>console.group()</code></a>.</p>
634<h4><code>console.groupEnd()</code><span><a class="mark" href="#consolegroupend" id="consolegroupend">#</a></span><a aria-hidden="true" class="legacy" id="console_console_groupend"></a></h4>
635<div class="api_metadata">
636<span>Added in: v8.5.0</span>
637</div>
638<p>Decreases indentation of subsequent lines by spaces for <code>groupIndentation</code>
639length.</p>
640<h4><code>console.info([data][, ...args])</code><span><a class="mark" href="#consoleinfodata-args" id="consoleinfodata-args">#</a></span><a aria-hidden="true" class="legacy" id="console_console_info_data_args"></a></h4>
641<div class="api_metadata">
642<span>Added in: v0.1.100</span>
643</div>
644<ul>
645<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
646<li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
647</ul>
648<p>The <code>console.info()</code> function is an alias for <a href="#consolelogdata-args"><code>console.log()</code></a>.</p>
649<h4><code>console.log([data][, ...args])</code><span><a class="mark" href="#consolelogdata-args" id="consolelogdata-args">#</a></span><a aria-hidden="true" class="legacy" id="console_console_log_data_args"></a></h4>
650<div class="api_metadata">
651<span>Added in: v0.1.100</span>
652</div>
653<ul>
654<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
655<li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
656</ul>
657<p>Prints to <code>stdout</code> with newline. Multiple arguments can be passed, with the
658first used as the primary message and all additional used as substitution
659values similar to <a href="http://man7.org/linux/man-pages/man3/printf.3.html"><code>printf(3)</code></a> (the arguments are all passed to
660<a href="util.html#utilformatformat-args"><code>util.format()</code></a>).</p>
661<pre><code class="language-js"><span class="hljs-keyword">const</span> count = <span class="hljs-number">5</span>;
662<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'count: %d'</span>, count);
663<span class="hljs-comment">// Prints: count: 5, to stdout</span>
664<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'count:'</span>, count);
665<span class="hljs-comment">// Prints: count: 5, to stdout</span></code> <button class="copy-button">copy</button></pre>
666<p>See <a href="util.html#utilformatformat-args"><code>util.format()</code></a> for more information.</p>
667<h4><code>console.table(tabularData[, properties])</code><span><a class="mark" href="#consoletabletabulardata-properties" id="consoletabletabulardata-properties">#</a></span><a aria-hidden="true" class="legacy" id="console_console_table_tabulardata_properties"></a></h4>
668<div class="api_metadata">
669<span>Added in: v10.0.0</span>
670</div>
671<ul>
672<li><code>tabularData</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
673<li><code>properties</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> Alternate properties for constructing the table.</li>
674</ul>
675<p>Try to construct a table with the columns of the properties of <code>tabularData</code>
676(or use <code>properties</code>) and rows of <code>tabularData</code> and log it. Falls back to just
677logging the argument if it can't be parsed as tabular.</p>
678<pre><code class="language-js"><span class="hljs-comment">// These can't be parsed as tabular data</span>
679<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">table</span>(<span class="hljs-title class_">Symbol</span>());
680<span class="hljs-comment">// Symbol()</span>
681
682<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">table</span>(<span class="hljs-literal">undefined</span>);
683<span class="hljs-comment">// undefined</span>
684
685<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">table</span>([{ <span class="hljs-attr">a</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">b</span>: <span class="hljs-string">'Y'</span> }, { <span class="hljs-attr">a</span>: <span class="hljs-string">'Z'</span>, <span class="hljs-attr">b</span>: <span class="hljs-number">2</span> }]);
686<span class="hljs-comment">// ┌─────────┬─────┬─────┐</span>
687<span class="hljs-comment">// │ (index) │  a  │  b  │</span>
688<span class="hljs-comment">// ├─────────┼─────┼─────┤</span>
689<span class="hljs-comment">// │    0    │  1  │ 'Y' │</span>
690<span class="hljs-comment">// │    1    │ 'Z' │  2  │</span>
691<span class="hljs-comment">// └─────────┴─────┴─────┘</span>
692
693<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">table</span>([{ <span class="hljs-attr">a</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">b</span>: <span class="hljs-string">'Y'</span> }, { <span class="hljs-attr">a</span>: <span class="hljs-string">'Z'</span>, <span class="hljs-attr">b</span>: <span class="hljs-number">2</span> }], [<span class="hljs-string">'a'</span>]);
694<span class="hljs-comment">// ┌─────────┬─────┐</span>
695<span class="hljs-comment">// │ (index) │  a  │</span>
696<span class="hljs-comment">// ├─────────┼─────┤</span>
697<span class="hljs-comment">// │    0    │  1  │</span>
698<span class="hljs-comment">// │    1    │ 'Z' │</span>
699<span class="hljs-comment">// └─────────┴─────┘</span></code> <button class="copy-button">copy</button></pre>
700<h4><code>console.time([label])</code><span><a class="mark" href="#consoletimelabel" id="consoletimelabel">#</a></span><a aria-hidden="true" class="legacy" id="console_console_time_label"></a></h4>
701<div class="api_metadata">
702<span>Added in: v0.1.104</span>
703</div>
704<ul>
705<li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <strong>Default:</strong> <code>'default'</code></li>
706</ul>
707<p>Starts a timer that can be used to compute the duration of an operation. Timers
708are identified by a unique <code>label</code>. Use the same <code>label</code> when calling
709<a href="#consoletimeendlabel"><code>console.timeEnd()</code></a> to stop the timer and output the elapsed time in
710suitable time units to <code>stdout</code>. For example, if the elapsed
711time is 3869ms, <code>console.timeEnd()</code> displays "3.869s".</p>
712<h4><code>console.timeEnd([label])</code><span><a class="mark" href="#consoletimeendlabel" id="consoletimeendlabel">#</a></span><a aria-hidden="true" class="legacy" id="console_console_timeend_label"></a></h4>
713<div class="api_metadata">
714<details class="changelog"><summary>History</summary>
715<table>
716<tbody><tr><th>Version</th><th>Changes</th></tr>
717<tr><td>v13.0.0</td>
718<td><p>The elapsed time is displayed with a suitable time unit.</p></td></tr>
719<tr><td>v6.0.0</td>
720<td><p>This method no longer supports multiple calls that don't map to individual <code>console.time()</code> calls; see below for details.</p></td></tr>
721<tr><td>v0.1.104</td>
722<td><p><span>Added in: v0.1.104</span></p></td></tr>
723</tbody></table>
724</details>
725</div>
726<ul>
727<li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <strong>Default:</strong> <code>'default'</code></li>
728</ul>
729<p>Stops a timer that was previously started by calling <a href="#consoletimelabel"><code>console.time()</code></a> and
730prints the result to <code>stdout</code>:</p>
731<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">time</span>(<span class="hljs-string">'bunch-of-stuff'</span>);
732<span class="hljs-comment">// Do a bunch of stuff.</span>
733<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">timeEnd</span>(<span class="hljs-string">'bunch-of-stuff'</span>);
734<span class="hljs-comment">// Prints: bunch-of-stuff: 225.438ms</span></code> <button class="copy-button">copy</button></pre>
735<h4><code>console.timeLog([label][, ...data])</code><span><a class="mark" href="#consoletimeloglabel-data" id="consoletimeloglabel-data">#</a></span><a aria-hidden="true" class="legacy" id="console_console_timelog_label_data"></a></h4>
736<div class="api_metadata">
737<span>Added in: v10.7.0</span>
738</div>
739<ul>
740<li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <strong>Default:</strong> <code>'default'</code></li>
741<li><code>...data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
742</ul>
743<p>For a timer that was previously started by calling <a href="#consoletimelabel"><code>console.time()</code></a>, prints
744the elapsed time and other <code>data</code> arguments to <code>stdout</code>:</p>
745<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">time</span>(<span class="hljs-string">'process'</span>);
746<span class="hljs-keyword">const</span> value = <span class="hljs-title function_">expensiveProcess1</span>(); <span class="hljs-comment">// Returns 42</span>
747<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">timeLog</span>(<span class="hljs-string">'process'</span>, value);
748<span class="hljs-comment">// Prints "process: 365.227ms 42".</span>
749<span class="hljs-title function_">doExpensiveProcess2</span>(value);
750<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">timeEnd</span>(<span class="hljs-string">'process'</span>);</code> <button class="copy-button">copy</button></pre>
751<h4><code>console.trace([message][, ...args])</code><span><a class="mark" href="#consoletracemessage-args" id="consoletracemessage-args">#</a></span><a aria-hidden="true" class="legacy" id="console_console_trace_message_args"></a></h4>
752<div class="api_metadata">
753<span>Added in: v0.1.104</span>
754</div>
755<ul>
756<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
757<li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
758</ul>
759<p>Prints to <code>stderr</code> the string <code>'Trace: '</code>, followed by the <a href="util.html#utilformatformat-args"><code>util.format()</code></a>
760formatted message and stack trace to the current position in the code.</p>
761<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">trace</span>(<span class="hljs-string">'Show me'</span>);
762<span class="hljs-comment">// Prints: (stack trace will vary based on where trace is called)</span>
763<span class="hljs-comment">//  Trace: Show me</span>
764<span class="hljs-comment">//    at repl:2:9</span>
765<span class="hljs-comment">//    at REPLServer.defaultEval (repl.js:248:27)</span>
766<span class="hljs-comment">//    at bound (domain.js:287:14)</span>
767<span class="hljs-comment">//    at REPLServer.runBound [as eval] (domain.js:300:12)</span>
768<span class="hljs-comment">//    at REPLServer.&#x3C;anonymous> (repl.js:412:12)</span>
769<span class="hljs-comment">//    at emitOne (events.js:82:20)</span>
770<span class="hljs-comment">//    at REPLServer.emit (events.js:169:7)</span>
771<span class="hljs-comment">//    at REPLServer.Interface._onLine (readline.js:210:10)</span>
772<span class="hljs-comment">//    at REPLServer.Interface._line (readline.js:549:8)</span>
773<span class="hljs-comment">//    at REPLServer.Interface._ttyWrite (readline.js:826:14)</span></code> <button class="copy-button">copy</button></pre>
774<h4><code>console.warn([data][, ...args])</code><span><a class="mark" href="#consolewarndata-args" id="consolewarndata-args">#</a></span><a aria-hidden="true" class="legacy" id="console_console_warn_data_args"></a></h4>
775<div class="api_metadata">
776<span>Added in: v0.1.100</span>
777</div>
778<ul>
779<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
780<li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
781</ul>
782<p>The <code>console.warn()</code> function is an alias for <a href="#consoleerrordata-args"><code>console.error()</code></a>.</p>
783</section><section><h3>Inspector only methods<span><a class="mark" href="#inspector-only-methods" id="inspector-only-methods">#</a></span><a aria-hidden="true" class="legacy" id="console_inspector_only_methods"></a></h3>
784<p>The following methods are exposed by the V8 engine in the general API but do
785not display anything unless used in conjunction with the <a href="debugger.html">inspector</a>
786(<code>--inspect</code> flag).</p>
787<h4><code>console.profile([label])</code><span><a class="mark" href="#consoleprofilelabel" id="consoleprofilelabel">#</a></span><a aria-hidden="true" class="legacy" id="console_console_profile_label"></a></h4>
788<div class="api_metadata">
789<span>Added in: v8.0.0</span>
790</div>
791<ul>
792<li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
793</ul>
794<p>This method does not display anything unless used in the inspector. The
795<code>console.profile()</code> method starts a JavaScript CPU profile with an optional
796label until <a href="#consoleprofileendlabel"><code>console.profileEnd()</code></a> is called. The profile is then added to
797the <strong>Profile</strong> panel of the inspector.</p>
798<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">profile</span>(<span class="hljs-string">'MyLabel'</span>);
799<span class="hljs-comment">// Some code</span>
800<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">profileEnd</span>(<span class="hljs-string">'MyLabel'</span>);
801<span class="hljs-comment">// Adds the profile 'MyLabel' to the Profiles panel of the inspector.</span></code> <button class="copy-button">copy</button></pre>
802<h4><code>console.profileEnd([label])</code><span><a class="mark" href="#consoleprofileendlabel" id="consoleprofileendlabel">#</a></span><a aria-hidden="true" class="legacy" id="console_console_profileend_label"></a></h4>
803<div class="api_metadata">
804<span>Added in: v8.0.0</span>
805</div>
806<ul>
807<li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
808</ul>
809<p>This method does not display anything unless used in the inspector. Stops the
810current JavaScript CPU profiling session if one has been started and prints
811the report to the <strong>Profiles</strong> panel of the inspector. See
812<a href="#consoleprofilelabel"><code>console.profile()</code></a> for an example.</p>
813<p>If this method is called without a label, the most recently started profile is
814stopped.</p>
815<h4><code>console.timeStamp([label])</code><span><a class="mark" href="#consoletimestamplabel" id="consoletimestamplabel">#</a></span><a aria-hidden="true" class="legacy" id="console_console_timestamp_label"></a></h4>
816<div class="api_metadata">
817<span>Added in: v8.0.0</span>
818</div>
819<ul>
820<li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
821</ul>
822<p>This method does not display anything unless used in the inspector. The
823<code>console.timeStamp()</code> method adds an event with the label <code>'label'</code> to the
824<strong>Timeline</strong> panel of the inspector.</p></section>
825        <!-- API END -->
826      </div>
827    </div>
828  </div>
829</body>
830</html>
831