Node.js Reference ยท Globals and Console
Node.js console.log() method
Reference for console.log() in Node.js: printing several values, printf-style format specifiers, how objects are inspected, and its siblings info and debug.
console.log() prints its arguments to standard output, separated by spaces and followed by a newline. Strings are printed as they are; every other value is formatted with util.inspect(), so objects, arrays, Maps and Sets appear in a readable form.
If the first argument is a string containing % format specifiers, the following arguments are substituted into it, much like printf. console.info() and console.debug() are aliases that behave exactly the same.
console.log('user %s has %d points', 'ann', 42);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
console.log([data[, ...args]]): void console.info([data[, ...args]]): void console.debug([data[, ...args]]): void
| Name | Description |
|---|---|
data |
The first value. If it is a string, it may contain format specifiers. |
...args |
Further values: used by the specifiers first, then appended with spaces. |
| Specifier | Meaning |
|---|---|
%s |
String. Numbers, bigints and booleans are converted; objects without a custom toString are inspected with limited depth. |
%d |
Number (including decimals). Objects and Symbols give NaN; bigints print as 123n. |
%i |
Integer: parses and truncates; NaN for non-numbers. |
%f |
Floating point: parseFloat of the argument. |
%j |
JSON text. [Circular] for circular structures. |
%o |
Inspect with showHidden: true, showProxy: true, depth 4. |
%O |
Inspect with default options. |
%c |
Consumes an argument and ignores it (browser CSS). |
%% |
A single percent sign. |
Returns: undefined. Errors: none for normal values; a throwing toString or getter on an inspected object can propagate.
Usage notes
- Goes to stdout. Use
console.error()for diagnostics so redirecting output (node app.js > out.txt) keeps them out of the data. - Format only happens when the first argument is a string and more arguments follow.
console.log(5, '%s', 'x')prints all three values untouched, andconsole.log('100%%')alone prints100%%. - Nested objects are cut off at depth 2 and shown as
[Object]. Useconsole.dir()withdepth: nullorutil.inspect()to see everything. - Strings inside objects are quoted; top-level strings are not. That makes
console.log('5')andconsole.log(5)look the same, so log with a label:console.log('count:', value). - Large arrays are shortened after 100 items with
... N more items; long strings in objects are not. - Writes are synchronous for files and TTYs and asynchronous for pipes on most platforms, so very chatty logging can slow a program and output may be lost if you call
process.exit()right after a pipe write. Prefer settingprocess.exitCode. - Logging is not free. For production, use a structured logger and levels rather than
console.login hot paths. util.format()applies the same rules and returns the string instead of printing it.