Skip to content

Ambiguity in docs on console.log and util.format #6341

Closed
@vsemozhetbyt

Description

@vsemozhetbyt
  • Subsystem: console, util

Here is a fragment from console docs:

If formatting elements (e.g. %d) are not found in the first string then util.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 with util.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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    consoleIssues and PRs related to the console subsystem.docIssues and PRs related to the documentations.good first issueIssues that are suitable for first-time contributors.utilIssues and PRs related to the built-in util module.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions