Skip to content

console, doc, util: console.log() and util.format() are wrongly documented and may be inconsistent #13908

Closed
@vsemozhetbyt

Description

@vsemozhetbyt

This is an issue that superseded #6341, Previously I thought this was a pure doc issue, now it seems to me that it is a more complicated problem that needs more discussion.

The both docs fragments (here and here) are wrong: util.inspect() is not called on each argument if the first argument is a non-format string. This is true if the first argument is not a string at all (see this path in code). If the first argument is a non-format string, util.inspect() is called only for arguments whose typeof is 'object' or 'symbol' (except null) — see this path in code.

Currently, I've found out that this impacts the output with String and Function arguments (watch out for quotes in the output for strings and absolutely different output for functions):

> console.log(1, 'str'); // .inspect()
1 'str'
> console.log('str', 'str'); // no .inspect()
str str

> console.log(1, () => true); // .inspect()
1 [Function]
> console.log('str', () => true); // no .inspect()
str () => true

Maybe there are other diferences.

Possible solutions:

  1. Document this difference.
  2. As it seems to be a confusing behavior, maybe we need to unify this with a semver-major fix.

UPD: This fragment is more correct:

> console.log(1, 'str2'); // .inspect()
1 'str2'
> console.log('str %s', 'str1', 'str2'); // no .inspect()
str str1 str2

> console.log(1, () => true); // .inspect()
1 [Function]
> console.log('str %s', 'str1', () => true); // no .inspect()
str str1 () => true

As you can see, util.inspect() is not called for excessive String and Function arguments here.

However, this doc fragment can also be improved, as functions are objects (maybe typeof should be mentioned).

Metadata

Metadata

Assignees

No one assigned

    Labels

    consoleIssues and PRs related to the console subsystem.docIssues and PRs related to the documentations.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