Skip to content

Clarify wording around "string representation" in console.* related functions #29172

Description

@jcbhmr
old text

MDN URL

https://developer.mozilla.org/en-US/docs/Web/API/console/assert

What specific section or headline is this issue about?

The part about passing in objects

What information was incorrect, unhelpful, or incomplete?

What should this print?

console.assert(false, { a: 1 })

In the spec text https://console.spec.whatwg.org/#assert it would seem (from my limited understanding) that it should forward everything to Logger() and it should print Assertion failed ▶ { a: 1 } like most other console methods do. That's what Chrome and Firefox do at least.

image
image

...but then on MDN there's this text: https://developer.mozilla.org/en-US/docs/Web/API/console/assert

image
image

which reads (to me at least; i could be mistaken) that passing console.assert(false, {a:1},{b:2}) will do String() or similar on each of those {a:1} objects
...which would seem to indicate that Node.js has the right idea:
image

idk man, I'm just a bit confused which one is "right" 😂

What did you expect to see?

Rephrase the text to be a bit more clear on whether "The string representations of each of these objects are appended together in the order listed and output." means perform string coercion or print a fancy object repr.

or idk maybe a little note or some screenshots of what happens sorta like https://developer.mozilla.org/en-US/docs/Web/API/console#using_groups_in_the_console which has a very nice image

or maybe a little note about how Node.js has a slightly different way of doing it 🤷‍♂️

Do you have any supporting links, references, or citations?

https://console.spec.whatwg.org/#assert

nodejs/node#49680

whatwg/console#77

Do you have anything more you want to share?

No response

Right now in a lot of the console.* methods there's this type of language used:
image

This is potentially confusing. It seems to indicate that said objects will all be .toString()-ed and concatenated together, sorta like this:

// obj1 … objN
// A list of JavaScript objects to output. The string representations of each
// of these objects are appended together in the order listed and output.
console.info = (...objN) => {
  underlyingPrint(objN.map(x => String(x)).join(" "))
}

but that's obviously NOT what it means to convey. Instead, it means to convey that these objects are "inspected" or "pretty printed" and then "shown" (not necessarily "printed") to the console in the specified order.

The issue at hand: Should this change?

If so, here's an idea:

before after

obj1 … objN
A list of JavaScript objects to output. The string representations of each of these objects are appended together in the order listed and output.

obj1 … objN
A list of non-string values to inspect in the console. The method of inspection is implementation defined. Browser consoles have interactive consoles, while text-based output may pretty-print the objects.

idk tho. something more clear like that

affected pages that i could find:

console.trace() has a pretty decent description https://developer.mozilla.org/en-US/docs/Web/API/console/trace

objects Optional
Zero or more objects to be output to console along with the trace. These are assembled and formatted the same way they would be if passed to the console.log() method.

Activity

  1. added
    needs triageTriage needed by staff and/or partners. Automatically applied when an issue is opened.
    on Sep 17, 2023
  2. changed the title [-]`console.assert(condition, ...data)` -- are `...data` elements coerced to a string or not?[/-] [+]What should `console.assert(condition, { a: 1 })` show in the console?[/+] on Sep 17, 2023
  3. changed the title [-]What should `console.assert(condition, { a: 1 })` show in the console?[/-] [+]What should `console.assert(false, { a: 1 })` show in the console?[/+] on Sep 17, 2023
  4. OnkarRuikar commented on Sep 18, 2023

    @OnkarRuikar
    Contributor

    2 Let message be a string without any formatting specifiers indicating generically an assertion failure (such as "Assertion failed").

    4.2 If Type(first) is not String, then prepend message to data.

    As per the specs data objects are to be appended to a string (message). So the NodeJS's output Assertion failed: [object Object] is correct as per the specs.

    const obj = {a:1};
    const message = "Assertion failed: " + obj;
    // Assertion failed: [object Object]
    

    Unlike NodeJs, browsers can show interactable objects in the logs. Because the console logs are part of the developer tools in browsers, it is a good feature to show the objects like that. It saves us from doing console.assert(false, JSON.strigify(obj)).

  5. jcbhmr commented on Sep 18, 2023

    @jcbhmr
    Author
  6. Josh-Cena commented on Sep 18, 2023

    @Josh-Cena
    Member

    Note that console spec is not a strict spec, per se—it's an attempt to find the greatest common divisor of all existing implementations. Therefore a lot of things are left very ambiguous, and so is the documentation... I'm inclined to say this is a divergence allowed in spec, whether the second arg should be a format string or a value to be directly printed.

  7. added
    needs infoNeeds more information to review or act on.
    and removed
    needs triageTriage needed by staff and/or partners. Automatically applied when an issue is opened.
    on Sep 18, 2023
  8. changed the title [-]What should `console.assert(false, { a: 1 })` show in the console?[/-] [+]Clarify wording around "string representation" in console.* related functions[/+] on Sep 19, 2023
  9. jcbhmr commented on Sep 19, 2023

    @jcbhmr
    Author

    I got my answer about "which is correct" from the whatwg/console folks: whatwg/console#226 (comment)

    I've switched this issue title/text to be focused on improving the wording about "string representation concatenated together" which is more on-topic as far as MDN content is concerned. 👍

    let me know your thoughts! ❤

  10. Josh-Cena commented on Sep 19, 2023

    @Josh-Cena
    Member

    I think the console docs in general would benefit from some analysis of how the input is interpreted and printed, but I don't think anyone is getting to it soon.

  11. added
    help wantedIf you know something about this topic, we would love your help!
    and removed
    needs infoNeeds more information to review or act on.
    on Sep 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Content:WebAPIWeb API docsarea: Consolehelp wantedIf you know something about this topic, we would love your help!

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions