Ambiguity in docs on console.log and util.format
- Subsystem: console, util
Here is a fragment from console docs:
If formatting elements (e.g.
%d) are not found in the first string thenutil.inspect()is called on each argument and the resulting string values are concatenated.
Here is a slightly different fragment from util docs:
If the first argument is not a format string then
util.format()returns a string that is the concatenation of all its arguments separated by spaces. Each argument is converted to a string withutil.inspect().
However, the behavior of both functions differs not only due to a presence of % elements in the string. According to the code (if I get it right), the behavior diverges due to merely typeof of the first argument.
Here is a test code with output confusing for the naive reader (including me):
const util = require('util'); console.log(util.inspect(1)); console.log(util.inspect('str')); console.log(); console.log(1, 'str'); console.log('str', 1); console.log(); console.log(util.format(1, 'str')); console.log(util.format('str', 1)); console.log();
The output:
1
'str'
1 'str'
str 1
1 'str'
str 1
The first block shows the elements for future concatenation. The second and third ones show the output of differently disposed arguments, with none of the strings containing the % format elements.