Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท util, assert and the Test Runner

Node.js util.format() function

Reference for util.format() in Node.js: the %s, %d, %i, %f, %j, %o, %O, %c and %% placeholders, extra arguments, and how console.log() uses it.

util.format(format, ...args) builds a string from a template containing placeholders such as %s and %d, in the style of C's printf(). It is the function behind console.log(), console.error() and friends, so the same placeholder rules apply when you log.

It is useful when you want that formatting without printing: building an error message, writing to a stream, or formatting a log line.

NODE
import { format } from "node:util";

format("%s is %d years old", "Ada", 36); // "Ada is 36 years old"

Try it

Node.js The placeholders

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 Extra and missing arguments

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
util.format(format?: any, ...args: any[]): string
Placeholder Meaning
%s String. String() for most values; BigInt gets an n suffix; objects without a custom toString() are shown through inspect() with depth 0; -0 stays -0.
%d Number (Number() conversion); BigInt gets an n; Symbol gives NaN.
%i parseInt() of the argument.
%f parseFloat() of the argument.
%j JSON.stringify(); circular structures give '[Circular]'.
%o inspect() with showHidden: true, showProxy: true and depth: 4 (shows functions' properties and hidden details).
%O inspect() without options (like the default console view).
%c Consumed and ignored (a CSS placeholder in browsers).
%% A literal percent sign.

Returns: the formatted string.

Edge cases: if the first argument is not a string, all arguments are converted with inspect() and joined with spaces. An unknown placeholder such as %x is left untouched and consumes no argument. A lone % at the end of the string is also left as is.

Usage notes

  • Remaining arguments are appended. Strings are added as they are, other values through inspect().
  • A call with only a format string is not processed. format("100%% sure") returns 100%% sure; %% becomes % only when at least one other argument is present.
  • A placeholder with no argument is left in the output, which makes missing data visible rather than printing undefined.
  • %s is not String() for objects. A plain object prints like [Object]-style inspect output (with depth 0), not [object Object]; objects with their own toString() (including Error) use it.
  • %d of a numeric string works, but %d with an object that has no number conversion gives NaN. Use %i for integers from strings like "42px".
  • No padding or width. Unlike C, %5d and %.2f are not supported; use toFixed(), padStart() or Intl.NumberFormat.
  • util.formatWithOptions(inspectOptions, ...) is the same function with inspect options (such as colors: true) applied to the inspect()-based parts.
  • Do not pass user input as the first argument to console.log() in logging code if it could contain % sequences; pass "%s", input instead.