util.format(format[, ...args])


  • format <string> 一个类似 printf 的格式字符串。

util.format() 方法返回一个格式化后的字符串,使用第一个参数作为一个类似 printf 的格式。

第一个参数是一个字符串,包含零个或多个占位符。 每个占位符会被对应参数转换后的值所替换。 支持的占位符有:

  • %s - 字符串。
  • %d - 数值(整数或浮点数)。
  • %i - Integer.
  • %f - Floating point value.
  • %j - JSON。如果参数包含循环引用,则用字符串 '[Circular]' 替换。
  • %o - Object. A string representation of an object with generic JavaScript object formatting. Similar to util.inspect() with options { showHidden: true, depth: 4, showProxy: true }. This will show the full object including non-enumerable symbols and properties.
  • %O - Object. A string representation of an object with generic JavaScript object formatting. Similar to util.inspect() without options. This will show the full object not including non-enumerable symbols and properties.
  • %% - 单个百分号('%')。不消耗参数。

如果占位符没有对应的参数,则占位符不被替换。

util.format('%s:%s', 'foo');
// 返回: 'foo:%s'

如果传入 util.format() 方法的参数比占位符的数量多,则多出的参数会被强制转换为字符串,然后拼接到返回的字符串,参数之间用一个空格分隔。 Excessive arguments whose typeof is 'object' or 'symbol' (except null) will be transformed by util.inspect().

util.format('%s:%s', 'foo', 'bar', 'baz'); // 'foo:bar baz'

如果第一个参数不是一个字符串,则 util.format() 返回一个所有参数用空格分隔并连在一起的字符串。 每个参数都使用 util.inspect() 转换为一个字符串。

util.format(1, 2, 3); // '1 2 3'

If only one argument is passed to util.format(), it is returned as it is without any formatting.

util.format('%% %s'); // '%% %s'
  • format <string> A printf-like format string.

The util.format() method returns a formatted string using the first argument as a printf-like format.

The first argument is a string containing zero or more placeholder tokens. Each placeholder token is replaced with the converted value from the corresponding argument. Supported placeholders are:

  • %s - String.
  • %d - Number (integer or floating point value) or BigInt.
  • %i - Integer or BigInt.
  • %f - Floating point value.
  • %j - JSON. Replaced with the string '[Circular]' if the argument contains circular references.
  • %o - Object. A string representation of an object with generic JavaScript object formatting. Similar to util.inspect() with options { showHidden: true, showProxy: true }. This will show the full object including non-enumerable properties and proxies.
  • %O - Object. A string representation of an object with generic JavaScript object formatting. Similar to util.inspect() without options. This will show the full object not including non-enumerable properties and proxies.
  • %% - single percent sign ('%'). This does not consume an argument.
  • Returns: <string> The formatted string

If the placeholder does not have a corresponding argument, the placeholder is not replaced.

util.format('%s:%s', 'foo');
// Returns: 'foo:%s'

If there are more arguments passed to the util.format() method than the number of placeholders, the extra arguments are coerced into strings then concatenated to the returned string, each delimited by a space. Excessive arguments whose typeof is 'object' or 'symbol' (except null) will be transformed by util.inspect().

util.format('%s:%s', 'foo', 'bar', 'baz'); // 'foo:bar baz'

If the first argument is not a string then util.format() returns a string that is the concatenation of all arguments separated by spaces. Each argument is converted to a string using util.inspect().

util.format(1, 2, 3); // '1 2 3'

If only one argument is passed to util.format(), it is returned as it is without any formatting.

util.format('%% %s'); // '%% %s'

Please note that util.format() is a synchronous method that is mainly intended as a debugging tool. Some input values can have a significant performance overhead that can block the event loop. Use this function with care and never in a hot code path.