util.format
History
%c specifier is ignored now.format argument is now only taken as such if it actually contains format specifiers.format argument is not a format string, the output string's formatting is no longer dependent on the type of the first argument. This change removes previously present quotes from strings that were being output when the first argument was not a string.%d, %f, and %i specifiers now support Symbols properly.%o specifier's depth has default depth of 4 again.%o specifier's depth option will now fall back to the default depth.%d and %i specifiers now support BigInt.%o and %O specifiers are supported now.util.format(format, ...args?): void
stringprintf-like format string.The util.format() method returns a formatted string using the first argument
as a printf-like format string which can contain zero or more format
specifiers. Each specifier is replaced with the converted value from the
corresponding argument. Supported specifiers are:
%s:Stringwill be used to convert all values exceptBigInt,Objectand-0.BigIntvalues will be represented with annand Objects that have neither a user definedtoStringfunction norSymbol.toPrimitivefunction are inspected usingutil.inspect()with options{ depth: 0, colors: false, compact: 3 }.%d:Numberwill be used to convert all values exceptBigIntandSymbol.%i:parseInt(value, 10)is used for all values exceptBigIntandSymbol.%f:parseFloat(value)is used for all values exceptSymbol.%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 toutil.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 toutil.inspect()without options. This will show the full object not including non-enumerable properties and proxies.%c:CSS. This specifier is ignored and will skip any CSS passed in.%%: single percent sign ('%'). This does not consume an argument.- Returns:
stringThe formatted string
If a specifier does not have a corresponding argument, it is not replaced:
util.format('%s:%s', 'foo'); // Returns: 'foo:%s'
Values that are not part of the format string are formatted using
util.inspect() if their type is not string.
If there are more arguments passed to the util.format() method than the
number of specifiers, the extra arguments are concatenated to the returned
string, separated by spaces:
util.format('%s:%s', 'foo', 'bar', 'baz'); // Returns: 'foo:bar baz'
If the first argument does not contain a valid format specifier, util.format()
returns a string that is the concatenation of all arguments separated by spaces:
util.format(1, 2, 3); // Returns: '1 2 3'
If only one argument is passed to util.format(), it is returned as it is
without any formatting:
util.format('%% %s'); // Returns: '%% %s'
util.format() is a synchronous method that is 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.