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.
import { format } from "node:util"; format("%s is %d years old", "Ada", 36); // "Ada is 36 years old"
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
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")returns100%% 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. %sis notString()for objects. A plain object prints like[Object]-style inspect output (with depth 0), not[object Object]; objects with their owntoString()(includingError) use it.%dof a numeric string works, but%dwith an object that has no number conversion givesNaN. Use%ifor integers from strings like"42px".- No padding or width. Unlike C,
%5dand%.2fare not supported; usetoFixed(),padStart()orIntl.NumberFormat. util.formatWithOptions(inspectOptions, ...)is the same function with inspect options (such ascolors: true) applied to theinspect()-based parts.- Do not pass user input as the first argument to
console.log()in logging code if it could contain%sequences; pass"%s", inputinstead.
Related
util.inspect(),console.log()- Lessons: The util module