Skip to content
Tutorials Center
My learning

Topics

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.

NODE
console.log('user %s has %d points', 'ann', 42);

Try it

Node.js Values and objects

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.

Node.js Format specifiers

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

NODE
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, and console.log('100%%') alone prints 100%%.
  • Nested objects are cut off at depth 2 and shown as [Object]. Use console.dir() with depth: null or util.inspect() to see everything.
  • Strings inside objects are quoted; top-level strings are not. That makes console.log('5') and console.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 setting process.exitCode.
  • Logging is not free. For production, use a structured logger and levels rather than console.log in hot paths.
  • util.format() applies the same rules and returns the string instead of printing it.