xref: /third_party/node/doc/api/console.md (revision 1cb0ef41)
11cb0ef41Sopenharmony_ci# Console
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci<!--introduced_in=v0.10.13-->
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci> Stability: 2 - Stable
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci<!-- source_link=lib/console.js -->
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciThe `node:console` module provides a simple debugging console that is similar to
101cb0ef41Sopenharmony_cithe JavaScript console mechanism provided by web browsers.
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciThe module exports two specific components:
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci* A `Console` class with methods such as `console.log()`, `console.error()`, and
151cb0ef41Sopenharmony_ci  `console.warn()` that can be used to write to any Node.js stream.
161cb0ef41Sopenharmony_ci* A global `console` instance configured to write to [`process.stdout`][] and
171cb0ef41Sopenharmony_ci  [`process.stderr`][]. The global `console` can be used without calling
181cb0ef41Sopenharmony_ci  `require('node:console')`.
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci_**Warning**_: The global console object's methods are neither consistently
211cb0ef41Sopenharmony_cisynchronous like the browser APIs they resemble, nor are they consistently
221cb0ef41Sopenharmony_ciasynchronous like all other Node.js streams. See the [note on process I/O][] for
231cb0ef41Sopenharmony_cimore information.
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ciExample using the global `console`:
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci```js
281cb0ef41Sopenharmony_ciconsole.log('hello world');
291cb0ef41Sopenharmony_ci// Prints: hello world, to stdout
301cb0ef41Sopenharmony_ciconsole.log('hello %s', 'world');
311cb0ef41Sopenharmony_ci// Prints: hello world, to stdout
321cb0ef41Sopenharmony_ciconsole.error(new Error('Whoops, something bad happened'));
331cb0ef41Sopenharmony_ci// Prints error message and stack trace to stderr:
341cb0ef41Sopenharmony_ci//   Error: Whoops, something bad happened
351cb0ef41Sopenharmony_ci//     at [eval]:5:15
361cb0ef41Sopenharmony_ci//     at Script.runInThisContext (node:vm:132:18)
371cb0ef41Sopenharmony_ci//     at Object.runInThisContext (node:vm:309:38)
381cb0ef41Sopenharmony_ci//     at node:internal/process/execution:77:19
391cb0ef41Sopenharmony_ci//     at [eval]-wrapper:6:22
401cb0ef41Sopenharmony_ci//     at evalScript (node:internal/process/execution:76:60)
411cb0ef41Sopenharmony_ci//     at node:internal/main/eval_string:23:3
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ciconst name = 'Will Robinson';
441cb0ef41Sopenharmony_ciconsole.warn(`Danger ${name}! Danger!`);
451cb0ef41Sopenharmony_ci// Prints: Danger Will Robinson! Danger!, to stderr
461cb0ef41Sopenharmony_ci```
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ciExample using the `Console` class:
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci```js
511cb0ef41Sopenharmony_ciconst out = getStreamSomehow();
521cb0ef41Sopenharmony_ciconst err = getStreamSomehow();
531cb0ef41Sopenharmony_ciconst myConsole = new console.Console(out, err);
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_cimyConsole.log('hello world');
561cb0ef41Sopenharmony_ci// Prints: hello world, to out
571cb0ef41Sopenharmony_cimyConsole.log('hello %s', 'world');
581cb0ef41Sopenharmony_ci// Prints: hello world, to out
591cb0ef41Sopenharmony_cimyConsole.error(new Error('Whoops, something bad happened'));
601cb0ef41Sopenharmony_ci// Prints: [Error: Whoops, something bad happened], to err
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ciconst name = 'Will Robinson';
631cb0ef41Sopenharmony_cimyConsole.warn(`Danger ${name}! Danger!`);
641cb0ef41Sopenharmony_ci// Prints: Danger Will Robinson! Danger!, to err
651cb0ef41Sopenharmony_ci```
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci## Class: `Console`
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci<!-- YAML
701cb0ef41Sopenharmony_cichanges:
711cb0ef41Sopenharmony_ci  - version: v8.0.0
721cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/9744
731cb0ef41Sopenharmony_ci    description: Errors that occur while writing to the underlying streams
741cb0ef41Sopenharmony_ci                 will now be ignored by default.
751cb0ef41Sopenharmony_ci-->
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci<!--type=class-->
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ciThe `Console` class can be used to create a simple logger with configurable
801cb0ef41Sopenharmony_cioutput streams and can be accessed using either `require('node:console').Console`
811cb0ef41Sopenharmony_cior `console.Console` (or their destructured counterparts):
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci```js
841cb0ef41Sopenharmony_ciconst { Console } = require('node:console');
851cb0ef41Sopenharmony_ci```
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci```js
881cb0ef41Sopenharmony_ciconst { Console } = console;
891cb0ef41Sopenharmony_ci```
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci### `new Console(stdout[, stderr][, ignoreErrors])`
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci### `new Console(options)`
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci<!-- YAML
961cb0ef41Sopenharmony_cichanges:
971cb0ef41Sopenharmony_ci  - version:
981cb0ef41Sopenharmony_ci     - v14.2.0
991cb0ef41Sopenharmony_ci     - v12.17.0
1001cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/32964
1011cb0ef41Sopenharmony_ci    description: The `groupIndentation` option was introduced.
1021cb0ef41Sopenharmony_ci  - version: v11.7.0
1031cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/24978
1041cb0ef41Sopenharmony_ci    description: The `inspectOptions` option is introduced.
1051cb0ef41Sopenharmony_ci  - version: v10.0.0
1061cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/19372
1071cb0ef41Sopenharmony_ci    description: The `Console` constructor now supports an `options` argument,
1081cb0ef41Sopenharmony_ci                 and the `colorMode` option was introduced.
1091cb0ef41Sopenharmony_ci  - version: v8.0.0
1101cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/9744
1111cb0ef41Sopenharmony_ci    description: The `ignoreErrors` option was introduced.
1121cb0ef41Sopenharmony_ci-->
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ci* `options` {Object}
1151cb0ef41Sopenharmony_ci  * `stdout` {stream.Writable}
1161cb0ef41Sopenharmony_ci  * `stderr` {stream.Writable}
1171cb0ef41Sopenharmony_ci  * `ignoreErrors` {boolean} Ignore errors when writing to the underlying
1181cb0ef41Sopenharmony_ci    streams. **Default:** `true`.
1191cb0ef41Sopenharmony_ci  * `colorMode` {boolean|string} Set color support for this `Console` instance.
1201cb0ef41Sopenharmony_ci    Setting to `true` enables coloring while inspecting values. Setting to
1211cb0ef41Sopenharmony_ci    `false` disables coloring while inspecting values. Setting to
1221cb0ef41Sopenharmony_ci    `'auto'` makes color support depend on the value of the `isTTY` property
1231cb0ef41Sopenharmony_ci    and the value returned by `getColorDepth()` on the respective stream. This
1241cb0ef41Sopenharmony_ci    option can not be used, if `inspectOptions.colors` is set as well.
1251cb0ef41Sopenharmony_ci    **Default:** `'auto'`.
1261cb0ef41Sopenharmony_ci  * `inspectOptions` {Object} Specifies options that are passed along to
1271cb0ef41Sopenharmony_ci    [`util.inspect()`][].
1281cb0ef41Sopenharmony_ci  * `groupIndentation` {number} Set group indentation.
1291cb0ef41Sopenharmony_ci    **Default:** `2`.
1301cb0ef41Sopenharmony_ci
1311cb0ef41Sopenharmony_ciCreates a new `Console` with one or two writable stream instances. `stdout` is a
1321cb0ef41Sopenharmony_ciwritable stream to print log or info output. `stderr` is used for warning or
1331cb0ef41Sopenharmony_cierror output. If `stderr` is not provided, `stdout` is used for `stderr`.
1341cb0ef41Sopenharmony_ci
1351cb0ef41Sopenharmony_ci```js
1361cb0ef41Sopenharmony_ciconst output = fs.createWriteStream('./stdout.log');
1371cb0ef41Sopenharmony_ciconst errorOutput = fs.createWriteStream('./stderr.log');
1381cb0ef41Sopenharmony_ci// Custom simple logger
1391cb0ef41Sopenharmony_ciconst logger = new Console({ stdout: output, stderr: errorOutput });
1401cb0ef41Sopenharmony_ci// use it like console
1411cb0ef41Sopenharmony_ciconst count = 5;
1421cb0ef41Sopenharmony_cilogger.log('count: %d', count);
1431cb0ef41Sopenharmony_ci// In stdout.log: count 5
1441cb0ef41Sopenharmony_ci```
1451cb0ef41Sopenharmony_ci
1461cb0ef41Sopenharmony_ciThe global `console` is a special `Console` whose output is sent to
1471cb0ef41Sopenharmony_ci[`process.stdout`][] and [`process.stderr`][]. It is equivalent to calling:
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_ci```js
1501cb0ef41Sopenharmony_cinew Console({ stdout: process.stdout, stderr: process.stderr });
1511cb0ef41Sopenharmony_ci```
1521cb0ef41Sopenharmony_ci
1531cb0ef41Sopenharmony_ci### `console.assert(value[, ...message])`
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_ci<!-- YAML
1561cb0ef41Sopenharmony_ciadded: v0.1.101
1571cb0ef41Sopenharmony_cichanges:
1581cb0ef41Sopenharmony_ci  - version: v10.0.0
1591cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/17706
1601cb0ef41Sopenharmony_ci    description: The implementation is now spec compliant and does not throw
1611cb0ef41Sopenharmony_ci                 anymore.
1621cb0ef41Sopenharmony_ci-->
1631cb0ef41Sopenharmony_ci
1641cb0ef41Sopenharmony_ci* `value` {any} The value tested for being truthy.
1651cb0ef41Sopenharmony_ci* `...message` {any} All arguments besides `value` are used as error message.
1661cb0ef41Sopenharmony_ci
1671cb0ef41Sopenharmony_ci`console.assert()` writes a message if `value` is [falsy][] or omitted. It only
1681cb0ef41Sopenharmony_ciwrites a message and does not otherwise affect execution. The output always
1691cb0ef41Sopenharmony_cistarts with `"Assertion failed"`. If provided, `message` is formatted using
1701cb0ef41Sopenharmony_ci[`util.format()`][].
1711cb0ef41Sopenharmony_ci
1721cb0ef41Sopenharmony_ciIf `value` is [truthy][], nothing happens.
1731cb0ef41Sopenharmony_ci
1741cb0ef41Sopenharmony_ci```js
1751cb0ef41Sopenharmony_ciconsole.assert(true, 'does nothing');
1761cb0ef41Sopenharmony_ci
1771cb0ef41Sopenharmony_ciconsole.assert(false, 'Whoops %s work', 'didn\'t');
1781cb0ef41Sopenharmony_ci// Assertion failed: Whoops didn't work
1791cb0ef41Sopenharmony_ci
1801cb0ef41Sopenharmony_ciconsole.assert();
1811cb0ef41Sopenharmony_ci// Assertion failed
1821cb0ef41Sopenharmony_ci```
1831cb0ef41Sopenharmony_ci
1841cb0ef41Sopenharmony_ci### `console.clear()`
1851cb0ef41Sopenharmony_ci
1861cb0ef41Sopenharmony_ci<!-- YAML
1871cb0ef41Sopenharmony_ciadded: v8.3.0
1881cb0ef41Sopenharmony_ci-->
1891cb0ef41Sopenharmony_ci
1901cb0ef41Sopenharmony_ciWhen `stdout` is a TTY, calling `console.clear()` will attempt to clear the
1911cb0ef41Sopenharmony_ciTTY. When `stdout` is not a TTY, this method does nothing.
1921cb0ef41Sopenharmony_ci
1931cb0ef41Sopenharmony_ciThe specific operation of `console.clear()` can vary across operating systems
1941cb0ef41Sopenharmony_ciand terminal types. For most Linux operating systems, `console.clear()`
1951cb0ef41Sopenharmony_cioperates similarly to the `clear` shell command. On Windows, `console.clear()`
1961cb0ef41Sopenharmony_ciwill clear only the output in the current terminal viewport for the Node.js
1971cb0ef41Sopenharmony_cibinary.
1981cb0ef41Sopenharmony_ci
1991cb0ef41Sopenharmony_ci### `console.count([label])`
2001cb0ef41Sopenharmony_ci
2011cb0ef41Sopenharmony_ci<!-- YAML
2021cb0ef41Sopenharmony_ciadded: v8.3.0
2031cb0ef41Sopenharmony_ci-->
2041cb0ef41Sopenharmony_ci
2051cb0ef41Sopenharmony_ci* `label` {string} The display label for the counter. **Default:** `'default'`.
2061cb0ef41Sopenharmony_ci
2071cb0ef41Sopenharmony_ciMaintains an internal counter specific to `label` and outputs to `stdout` the
2081cb0ef41Sopenharmony_cinumber of times `console.count()` has been called with the given `label`.
2091cb0ef41Sopenharmony_ci
2101cb0ef41Sopenharmony_ci<!-- eslint-skip -->
2111cb0ef41Sopenharmony_ci
2121cb0ef41Sopenharmony_ci```js
2131cb0ef41Sopenharmony_ci> console.count()
2141cb0ef41Sopenharmony_cidefault: 1
2151cb0ef41Sopenharmony_ciundefined
2161cb0ef41Sopenharmony_ci> console.count('default')
2171cb0ef41Sopenharmony_cidefault: 2
2181cb0ef41Sopenharmony_ciundefined
2191cb0ef41Sopenharmony_ci> console.count('abc')
2201cb0ef41Sopenharmony_ciabc: 1
2211cb0ef41Sopenharmony_ciundefined
2221cb0ef41Sopenharmony_ci> console.count('xyz')
2231cb0ef41Sopenharmony_cixyz: 1
2241cb0ef41Sopenharmony_ciundefined
2251cb0ef41Sopenharmony_ci> console.count('abc')
2261cb0ef41Sopenharmony_ciabc: 2
2271cb0ef41Sopenharmony_ciundefined
2281cb0ef41Sopenharmony_ci> console.count()
2291cb0ef41Sopenharmony_cidefault: 3
2301cb0ef41Sopenharmony_ciundefined
2311cb0ef41Sopenharmony_ci>
2321cb0ef41Sopenharmony_ci```
2331cb0ef41Sopenharmony_ci
2341cb0ef41Sopenharmony_ci### `console.countReset([label])`
2351cb0ef41Sopenharmony_ci
2361cb0ef41Sopenharmony_ci<!-- YAML
2371cb0ef41Sopenharmony_ciadded: v8.3.0
2381cb0ef41Sopenharmony_ci-->
2391cb0ef41Sopenharmony_ci
2401cb0ef41Sopenharmony_ci* `label` {string} The display label for the counter. **Default:** `'default'`.
2411cb0ef41Sopenharmony_ci
2421cb0ef41Sopenharmony_ciResets the internal counter specific to `label`.
2431cb0ef41Sopenharmony_ci
2441cb0ef41Sopenharmony_ci<!-- eslint-skip -->
2451cb0ef41Sopenharmony_ci
2461cb0ef41Sopenharmony_ci```js
2471cb0ef41Sopenharmony_ci> console.count('abc');
2481cb0ef41Sopenharmony_ciabc: 1
2491cb0ef41Sopenharmony_ciundefined
2501cb0ef41Sopenharmony_ci> console.countReset('abc');
2511cb0ef41Sopenharmony_ciundefined
2521cb0ef41Sopenharmony_ci> console.count('abc');
2531cb0ef41Sopenharmony_ciabc: 1
2541cb0ef41Sopenharmony_ciundefined
2551cb0ef41Sopenharmony_ci>
2561cb0ef41Sopenharmony_ci```
2571cb0ef41Sopenharmony_ci
2581cb0ef41Sopenharmony_ci### `console.debug(data[, ...args])`
2591cb0ef41Sopenharmony_ci
2601cb0ef41Sopenharmony_ci<!-- YAML
2611cb0ef41Sopenharmony_ciadded: v8.0.0
2621cb0ef41Sopenharmony_cichanges:
2631cb0ef41Sopenharmony_ci  - version: v8.10.0
2641cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/17033
2651cb0ef41Sopenharmony_ci    description: "`console.debug` is now an alias for `console.log`."
2661cb0ef41Sopenharmony_ci-->
2671cb0ef41Sopenharmony_ci
2681cb0ef41Sopenharmony_ci* `data` {any}
2691cb0ef41Sopenharmony_ci* `...args` {any}
2701cb0ef41Sopenharmony_ci
2711cb0ef41Sopenharmony_ciThe `console.debug()` function is an alias for [`console.log()`][].
2721cb0ef41Sopenharmony_ci
2731cb0ef41Sopenharmony_ci### `console.dir(obj[, options])`
2741cb0ef41Sopenharmony_ci
2751cb0ef41Sopenharmony_ci<!-- YAML
2761cb0ef41Sopenharmony_ciadded: v0.1.101
2771cb0ef41Sopenharmony_ci-->
2781cb0ef41Sopenharmony_ci
2791cb0ef41Sopenharmony_ci* `obj` {any}
2801cb0ef41Sopenharmony_ci* `options` {Object}
2811cb0ef41Sopenharmony_ci  * `showHidden` {boolean} If `true` then the object's non-enumerable and symbol
2821cb0ef41Sopenharmony_ci    properties will be shown too. **Default:** `false`.
2831cb0ef41Sopenharmony_ci  * `depth` {number} Tells [`util.inspect()`][] how many times to recurse while
2841cb0ef41Sopenharmony_ci    formatting the object. This is useful for inspecting large complicated
2851cb0ef41Sopenharmony_ci    objects. To make it recurse indefinitely, pass `null`. **Default:** `2`.
2861cb0ef41Sopenharmony_ci  * `colors` {boolean} If `true`, then the output will be styled with ANSI color
2871cb0ef41Sopenharmony_ci    codes. Colors are customizable;
2881cb0ef41Sopenharmony_ci    see [customizing `util.inspect()` colors][]. **Default:** `false`.
2891cb0ef41Sopenharmony_ci
2901cb0ef41Sopenharmony_ciUses [`util.inspect()`][] on `obj` and prints the resulting string to `stdout`.
2911cb0ef41Sopenharmony_ciThis function bypasses any custom `inspect()` function defined on `obj`.
2921cb0ef41Sopenharmony_ci
2931cb0ef41Sopenharmony_ci### `console.dirxml(...data)`
2941cb0ef41Sopenharmony_ci
2951cb0ef41Sopenharmony_ci<!-- YAML
2961cb0ef41Sopenharmony_ciadded: v8.0.0
2971cb0ef41Sopenharmony_cichanges:
2981cb0ef41Sopenharmony_ci  - version: v9.3.0
2991cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/17152
3001cb0ef41Sopenharmony_ci    description: "`console.dirxml` now calls `console.log` for its arguments."
3011cb0ef41Sopenharmony_ci-->
3021cb0ef41Sopenharmony_ci
3031cb0ef41Sopenharmony_ci* `...data` {any}
3041cb0ef41Sopenharmony_ci
3051cb0ef41Sopenharmony_ciThis method calls `console.log()` passing it the arguments received.
3061cb0ef41Sopenharmony_ciThis method does not produce any XML formatting.
3071cb0ef41Sopenharmony_ci
3081cb0ef41Sopenharmony_ci### `console.error([data][, ...args])`
3091cb0ef41Sopenharmony_ci
3101cb0ef41Sopenharmony_ci<!-- YAML
3111cb0ef41Sopenharmony_ciadded: v0.1.100
3121cb0ef41Sopenharmony_ci-->
3131cb0ef41Sopenharmony_ci
3141cb0ef41Sopenharmony_ci* `data` {any}
3151cb0ef41Sopenharmony_ci* `...args` {any}
3161cb0ef41Sopenharmony_ci
3171cb0ef41Sopenharmony_ciPrints to `stderr` with newline. Multiple arguments can be passed, with the
3181cb0ef41Sopenharmony_cifirst used as the primary message and all additional used as substitution
3191cb0ef41Sopenharmony_civalues similar to printf(3) (the arguments are all passed to
3201cb0ef41Sopenharmony_ci[`util.format()`][]).
3211cb0ef41Sopenharmony_ci
3221cb0ef41Sopenharmony_ci```js
3231cb0ef41Sopenharmony_ciconst code = 5;
3241cb0ef41Sopenharmony_ciconsole.error('error #%d', code);
3251cb0ef41Sopenharmony_ci// Prints: error #5, to stderr
3261cb0ef41Sopenharmony_ciconsole.error('error', code);
3271cb0ef41Sopenharmony_ci// Prints: error 5, to stderr
3281cb0ef41Sopenharmony_ci```
3291cb0ef41Sopenharmony_ci
3301cb0ef41Sopenharmony_ciIf formatting elements (e.g. `%d`) are not found in the first string then
3311cb0ef41Sopenharmony_ci[`util.inspect()`][] is called on each argument and the resulting string
3321cb0ef41Sopenharmony_civalues are concatenated. See [`util.format()`][] for more information.
3331cb0ef41Sopenharmony_ci
3341cb0ef41Sopenharmony_ci### `console.group([...label])`
3351cb0ef41Sopenharmony_ci
3361cb0ef41Sopenharmony_ci<!-- YAML
3371cb0ef41Sopenharmony_ciadded: v8.5.0
3381cb0ef41Sopenharmony_ci-->
3391cb0ef41Sopenharmony_ci
3401cb0ef41Sopenharmony_ci* `...label` {any}
3411cb0ef41Sopenharmony_ci
3421cb0ef41Sopenharmony_ciIncreases indentation of subsequent lines by spaces for `groupIndentation`
3431cb0ef41Sopenharmony_cilength.
3441cb0ef41Sopenharmony_ci
3451cb0ef41Sopenharmony_ciIf one or more `label`s are provided, those are printed first without the
3461cb0ef41Sopenharmony_ciadditional indentation.
3471cb0ef41Sopenharmony_ci
3481cb0ef41Sopenharmony_ci### `console.groupCollapsed()`
3491cb0ef41Sopenharmony_ci
3501cb0ef41Sopenharmony_ci<!-- YAML
3511cb0ef41Sopenharmony_ci  added: v8.5.0
3521cb0ef41Sopenharmony_ci-->
3531cb0ef41Sopenharmony_ci
3541cb0ef41Sopenharmony_ciAn alias for [`console.group()`][].
3551cb0ef41Sopenharmony_ci
3561cb0ef41Sopenharmony_ci### `console.groupEnd()`
3571cb0ef41Sopenharmony_ci
3581cb0ef41Sopenharmony_ci<!-- YAML
3591cb0ef41Sopenharmony_ciadded: v8.5.0
3601cb0ef41Sopenharmony_ci-->
3611cb0ef41Sopenharmony_ci
3621cb0ef41Sopenharmony_ciDecreases indentation of subsequent lines by spaces for `groupIndentation`
3631cb0ef41Sopenharmony_cilength.
3641cb0ef41Sopenharmony_ci
3651cb0ef41Sopenharmony_ci### `console.info([data][, ...args])`
3661cb0ef41Sopenharmony_ci
3671cb0ef41Sopenharmony_ci<!-- YAML
3681cb0ef41Sopenharmony_ciadded: v0.1.100
3691cb0ef41Sopenharmony_ci-->
3701cb0ef41Sopenharmony_ci
3711cb0ef41Sopenharmony_ci* `data` {any}
3721cb0ef41Sopenharmony_ci* `...args` {any}
3731cb0ef41Sopenharmony_ci
3741cb0ef41Sopenharmony_ciThe `console.info()` function is an alias for [`console.log()`][].
3751cb0ef41Sopenharmony_ci
3761cb0ef41Sopenharmony_ci### `console.log([data][, ...args])`
3771cb0ef41Sopenharmony_ci
3781cb0ef41Sopenharmony_ci<!-- YAML
3791cb0ef41Sopenharmony_ciadded: v0.1.100
3801cb0ef41Sopenharmony_ci-->
3811cb0ef41Sopenharmony_ci
3821cb0ef41Sopenharmony_ci* `data` {any}
3831cb0ef41Sopenharmony_ci* `...args` {any}
3841cb0ef41Sopenharmony_ci
3851cb0ef41Sopenharmony_ciPrints to `stdout` with newline. Multiple arguments can be passed, with the
3861cb0ef41Sopenharmony_cifirst used as the primary message and all additional used as substitution
3871cb0ef41Sopenharmony_civalues similar to printf(3) (the arguments are all passed to
3881cb0ef41Sopenharmony_ci[`util.format()`][]).
3891cb0ef41Sopenharmony_ci
3901cb0ef41Sopenharmony_ci```js
3911cb0ef41Sopenharmony_ciconst count = 5;
3921cb0ef41Sopenharmony_ciconsole.log('count: %d', count);
3931cb0ef41Sopenharmony_ci// Prints: count: 5, to stdout
3941cb0ef41Sopenharmony_ciconsole.log('count:', count);
3951cb0ef41Sopenharmony_ci// Prints: count: 5, to stdout
3961cb0ef41Sopenharmony_ci```
3971cb0ef41Sopenharmony_ci
3981cb0ef41Sopenharmony_ciSee [`util.format()`][] for more information.
3991cb0ef41Sopenharmony_ci
4001cb0ef41Sopenharmony_ci### `console.table(tabularData[, properties])`
4011cb0ef41Sopenharmony_ci
4021cb0ef41Sopenharmony_ci<!-- YAML
4031cb0ef41Sopenharmony_ciadded: v10.0.0
4041cb0ef41Sopenharmony_ci-->
4051cb0ef41Sopenharmony_ci
4061cb0ef41Sopenharmony_ci* `tabularData` {any}
4071cb0ef41Sopenharmony_ci* `properties` {string\[]} Alternate properties for constructing the table.
4081cb0ef41Sopenharmony_ci
4091cb0ef41Sopenharmony_ciTry to construct a table with the columns of the properties of `tabularData`
4101cb0ef41Sopenharmony_ci(or use `properties`) and rows of `tabularData` and log it. Falls back to just
4111cb0ef41Sopenharmony_cilogging the argument if it can't be parsed as tabular.
4121cb0ef41Sopenharmony_ci
4131cb0ef41Sopenharmony_ci```js
4141cb0ef41Sopenharmony_ci// These can't be parsed as tabular data
4151cb0ef41Sopenharmony_ciconsole.table(Symbol());
4161cb0ef41Sopenharmony_ci// Symbol()
4171cb0ef41Sopenharmony_ci
4181cb0ef41Sopenharmony_ciconsole.table(undefined);
4191cb0ef41Sopenharmony_ci// undefined
4201cb0ef41Sopenharmony_ci
4211cb0ef41Sopenharmony_ciconsole.table([{ a: 1, b: 'Y' }, { a: 'Z', b: 2 }]);
4221cb0ef41Sopenharmony_ci// ┌─────────┬─────┬─────┐
4231cb0ef41Sopenharmony_ci// │ (index) │  a  │  b  │
4241cb0ef41Sopenharmony_ci// ├─────────┼─────┼─────┤
4251cb0ef41Sopenharmony_ci// │    0    │  1  │ 'Y' │
4261cb0ef41Sopenharmony_ci// │    1    │ 'Z' │  2  │
4271cb0ef41Sopenharmony_ci// └─────────┴─────┴─────┘
4281cb0ef41Sopenharmony_ci
4291cb0ef41Sopenharmony_ciconsole.table([{ a: 1, b: 'Y' }, { a: 'Z', b: 2 }], ['a']);
4301cb0ef41Sopenharmony_ci// ┌─────────┬─────┐
4311cb0ef41Sopenharmony_ci// │ (index) │  a  │
4321cb0ef41Sopenharmony_ci// ├─────────┼─────┤
4331cb0ef41Sopenharmony_ci// │    0    │  1  │
4341cb0ef41Sopenharmony_ci// │    1    │ 'Z' │
4351cb0ef41Sopenharmony_ci// └─────────┴─────┘
4361cb0ef41Sopenharmony_ci```
4371cb0ef41Sopenharmony_ci
4381cb0ef41Sopenharmony_ci### `console.time([label])`
4391cb0ef41Sopenharmony_ci
4401cb0ef41Sopenharmony_ci<!-- YAML
4411cb0ef41Sopenharmony_ciadded: v0.1.104
4421cb0ef41Sopenharmony_ci-->
4431cb0ef41Sopenharmony_ci
4441cb0ef41Sopenharmony_ci* `label` {string} **Default:** `'default'`
4451cb0ef41Sopenharmony_ci
4461cb0ef41Sopenharmony_ciStarts a timer that can be used to compute the duration of an operation. Timers
4471cb0ef41Sopenharmony_ciare identified by a unique `label`. Use the same `label` when calling
4481cb0ef41Sopenharmony_ci[`console.timeEnd()`][] to stop the timer and output the elapsed time in
4491cb0ef41Sopenharmony_cisuitable time units to `stdout`. For example, if the elapsed
4501cb0ef41Sopenharmony_citime is 3869ms, `console.timeEnd()` displays "3.869s".
4511cb0ef41Sopenharmony_ci
4521cb0ef41Sopenharmony_ci### `console.timeEnd([label])`
4531cb0ef41Sopenharmony_ci
4541cb0ef41Sopenharmony_ci<!-- YAML
4551cb0ef41Sopenharmony_ciadded: v0.1.104
4561cb0ef41Sopenharmony_cichanges:
4571cb0ef41Sopenharmony_ci  - version: v13.0.0
4581cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/29251
4591cb0ef41Sopenharmony_ci    description: The elapsed time is displayed with a suitable time unit.
4601cb0ef41Sopenharmony_ci  - version: v6.0.0
4611cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/5901
4621cb0ef41Sopenharmony_ci    description: This method no longer supports multiple calls that don't map
4631cb0ef41Sopenharmony_ci                 to individual `console.time()` calls; see below for details.
4641cb0ef41Sopenharmony_ci-->
4651cb0ef41Sopenharmony_ci
4661cb0ef41Sopenharmony_ci* `label` {string} **Default:** `'default'`
4671cb0ef41Sopenharmony_ci
4681cb0ef41Sopenharmony_ciStops a timer that was previously started by calling [`console.time()`][] and
4691cb0ef41Sopenharmony_ciprints the result to `stdout`:
4701cb0ef41Sopenharmony_ci
4711cb0ef41Sopenharmony_ci```js
4721cb0ef41Sopenharmony_ciconsole.time('bunch-of-stuff');
4731cb0ef41Sopenharmony_ci// Do a bunch of stuff.
4741cb0ef41Sopenharmony_ciconsole.timeEnd('bunch-of-stuff');
4751cb0ef41Sopenharmony_ci// Prints: bunch-of-stuff: 225.438ms
4761cb0ef41Sopenharmony_ci```
4771cb0ef41Sopenharmony_ci
4781cb0ef41Sopenharmony_ci### `console.timeLog([label][, ...data])`
4791cb0ef41Sopenharmony_ci
4801cb0ef41Sopenharmony_ci<!-- YAML
4811cb0ef41Sopenharmony_ciadded: v10.7.0
4821cb0ef41Sopenharmony_ci-->
4831cb0ef41Sopenharmony_ci
4841cb0ef41Sopenharmony_ci* `label` {string} **Default:** `'default'`
4851cb0ef41Sopenharmony_ci* `...data` {any}
4861cb0ef41Sopenharmony_ci
4871cb0ef41Sopenharmony_ciFor a timer that was previously started by calling [`console.time()`][], prints
4881cb0ef41Sopenharmony_cithe elapsed time and other `data` arguments to `stdout`:
4891cb0ef41Sopenharmony_ci
4901cb0ef41Sopenharmony_ci```js
4911cb0ef41Sopenharmony_ciconsole.time('process');
4921cb0ef41Sopenharmony_ciconst value = expensiveProcess1(); // Returns 42
4931cb0ef41Sopenharmony_ciconsole.timeLog('process', value);
4941cb0ef41Sopenharmony_ci// Prints "process: 365.227ms 42".
4951cb0ef41Sopenharmony_cidoExpensiveProcess2(value);
4961cb0ef41Sopenharmony_ciconsole.timeEnd('process');
4971cb0ef41Sopenharmony_ci```
4981cb0ef41Sopenharmony_ci
4991cb0ef41Sopenharmony_ci### `console.trace([message][, ...args])`
5001cb0ef41Sopenharmony_ci
5011cb0ef41Sopenharmony_ci<!-- YAML
5021cb0ef41Sopenharmony_ciadded: v0.1.104
5031cb0ef41Sopenharmony_ci-->
5041cb0ef41Sopenharmony_ci
5051cb0ef41Sopenharmony_ci* `message` {any}
5061cb0ef41Sopenharmony_ci* `...args` {any}
5071cb0ef41Sopenharmony_ci
5081cb0ef41Sopenharmony_ciPrints to `stderr` the string `'Trace: '`, followed by the [`util.format()`][]
5091cb0ef41Sopenharmony_ciformatted message and stack trace to the current position in the code.
5101cb0ef41Sopenharmony_ci
5111cb0ef41Sopenharmony_ci```js
5121cb0ef41Sopenharmony_ciconsole.trace('Show me');
5131cb0ef41Sopenharmony_ci// Prints: (stack trace will vary based on where trace is called)
5141cb0ef41Sopenharmony_ci//  Trace: Show me
5151cb0ef41Sopenharmony_ci//    at repl:2:9
5161cb0ef41Sopenharmony_ci//    at REPLServer.defaultEval (repl.js:248:27)
5171cb0ef41Sopenharmony_ci//    at bound (domain.js:287:14)
5181cb0ef41Sopenharmony_ci//    at REPLServer.runBound [as eval] (domain.js:300:12)
5191cb0ef41Sopenharmony_ci//    at REPLServer.<anonymous> (repl.js:412:12)
5201cb0ef41Sopenharmony_ci//    at emitOne (events.js:82:20)
5211cb0ef41Sopenharmony_ci//    at REPLServer.emit (events.js:169:7)
5221cb0ef41Sopenharmony_ci//    at REPLServer.Interface._onLine (readline.js:210:10)
5231cb0ef41Sopenharmony_ci//    at REPLServer.Interface._line (readline.js:549:8)
5241cb0ef41Sopenharmony_ci//    at REPLServer.Interface._ttyWrite (readline.js:826:14)
5251cb0ef41Sopenharmony_ci```
5261cb0ef41Sopenharmony_ci
5271cb0ef41Sopenharmony_ci### `console.warn([data][, ...args])`
5281cb0ef41Sopenharmony_ci
5291cb0ef41Sopenharmony_ci<!-- YAML
5301cb0ef41Sopenharmony_ciadded: v0.1.100
5311cb0ef41Sopenharmony_ci-->
5321cb0ef41Sopenharmony_ci
5331cb0ef41Sopenharmony_ci* `data` {any}
5341cb0ef41Sopenharmony_ci* `...args` {any}
5351cb0ef41Sopenharmony_ci
5361cb0ef41Sopenharmony_ciThe `console.warn()` function is an alias for [`console.error()`][].
5371cb0ef41Sopenharmony_ci
5381cb0ef41Sopenharmony_ci## Inspector only methods
5391cb0ef41Sopenharmony_ci
5401cb0ef41Sopenharmony_ciThe following methods are exposed by the V8 engine in the general API but do
5411cb0ef41Sopenharmony_cinot display anything unless used in conjunction with the [inspector][]
5421cb0ef41Sopenharmony_ci(`--inspect` flag).
5431cb0ef41Sopenharmony_ci
5441cb0ef41Sopenharmony_ci### `console.profile([label])`
5451cb0ef41Sopenharmony_ci
5461cb0ef41Sopenharmony_ci<!-- YAML
5471cb0ef41Sopenharmony_ciadded: v8.0.0
5481cb0ef41Sopenharmony_ci-->
5491cb0ef41Sopenharmony_ci
5501cb0ef41Sopenharmony_ci* `label` {string}
5511cb0ef41Sopenharmony_ci
5521cb0ef41Sopenharmony_ciThis method does not display anything unless used in the inspector. The
5531cb0ef41Sopenharmony_ci`console.profile()` method starts a JavaScript CPU profile with an optional
5541cb0ef41Sopenharmony_cilabel until [`console.profileEnd()`][] is called. The profile is then added to
5551cb0ef41Sopenharmony_cithe **Profile** panel of the inspector.
5561cb0ef41Sopenharmony_ci
5571cb0ef41Sopenharmony_ci```js
5581cb0ef41Sopenharmony_ciconsole.profile('MyLabel');
5591cb0ef41Sopenharmony_ci// Some code
5601cb0ef41Sopenharmony_ciconsole.profileEnd('MyLabel');
5611cb0ef41Sopenharmony_ci// Adds the profile 'MyLabel' to the Profiles panel of the inspector.
5621cb0ef41Sopenharmony_ci```
5631cb0ef41Sopenharmony_ci
5641cb0ef41Sopenharmony_ci### `console.profileEnd([label])`
5651cb0ef41Sopenharmony_ci
5661cb0ef41Sopenharmony_ci<!-- YAML
5671cb0ef41Sopenharmony_ciadded: v8.0.0
5681cb0ef41Sopenharmony_ci-->
5691cb0ef41Sopenharmony_ci
5701cb0ef41Sopenharmony_ci* `label` {string}
5711cb0ef41Sopenharmony_ci
5721cb0ef41Sopenharmony_ciThis method does not display anything unless used in the inspector. Stops the
5731cb0ef41Sopenharmony_cicurrent JavaScript CPU profiling session if one has been started and prints
5741cb0ef41Sopenharmony_cithe report to the **Profiles** panel of the inspector. See
5751cb0ef41Sopenharmony_ci[`console.profile()`][] for an example.
5761cb0ef41Sopenharmony_ci
5771cb0ef41Sopenharmony_ciIf this method is called without a label, the most recently started profile is
5781cb0ef41Sopenharmony_cistopped.
5791cb0ef41Sopenharmony_ci
5801cb0ef41Sopenharmony_ci### `console.timeStamp([label])`
5811cb0ef41Sopenharmony_ci
5821cb0ef41Sopenharmony_ci<!-- YAML
5831cb0ef41Sopenharmony_ciadded: v8.0.0
5841cb0ef41Sopenharmony_ci-->
5851cb0ef41Sopenharmony_ci
5861cb0ef41Sopenharmony_ci* `label` {string}
5871cb0ef41Sopenharmony_ci
5881cb0ef41Sopenharmony_ciThis method does not display anything unless used in the inspector. The
5891cb0ef41Sopenharmony_ci`console.timeStamp()` method adds an event with the label `'label'` to the
5901cb0ef41Sopenharmony_ci**Timeline** panel of the inspector.
5911cb0ef41Sopenharmony_ci
5921cb0ef41Sopenharmony_ci[`console.error()`]: #consoleerrordata-args
5931cb0ef41Sopenharmony_ci[`console.group()`]: #consolegrouplabel
5941cb0ef41Sopenharmony_ci[`console.log()`]: #consolelogdata-args
5951cb0ef41Sopenharmony_ci[`console.profile()`]: #consoleprofilelabel
5961cb0ef41Sopenharmony_ci[`console.profileEnd()`]: #consoleprofileendlabel
5971cb0ef41Sopenharmony_ci[`console.time()`]: #consoletimelabel
5981cb0ef41Sopenharmony_ci[`console.timeEnd()`]: #consoletimeendlabel
5991cb0ef41Sopenharmony_ci[`process.stderr`]: process.md#processstderr
6001cb0ef41Sopenharmony_ci[`process.stdout`]: process.md#processstdout
6011cb0ef41Sopenharmony_ci[`util.format()`]: util.md#utilformatformat-args
6021cb0ef41Sopenharmony_ci[`util.inspect()`]: util.md#utilinspectobject-options
6031cb0ef41Sopenharmony_ci[customizing `util.inspect()` colors]: util.md#customizing-utilinspect-colors
6041cb0ef41Sopenharmony_ci[falsy]: https://developer.mozilla.org/en-US/docs/Glossary/Falsy
6051cb0ef41Sopenharmony_ci[inspector]: debugger.md
6061cb0ef41Sopenharmony_ci[note on process I/O]: process.md#a-note-on-process-io
6071cb0ef41Sopenharmony_ci[truthy]: https://developer.mozilla.org/en-US/docs/Glossary/Truthy
608