0

In JavaScript The definitive guide 7th edition, page 317, The Console API the author clearly says that console.log():

...converts its arguments to strings and outputs them to the console.

However, MDN says that:

A representation of each of these values is output to the console in the order given with some type of separation between each of them.

Further research has revealed that the display of the arguments in the console is implementation-dependent. I am inclined to disagree with the author(s) of The definitive guide in this situation, however I want to be sure that they are wrong on this particular occasion.

I have checked the Console API "specification" and it didn't say anything about the arguments being converted to strings.

5
  • 5
    Well, try console.log({foo: 'bar'}). For me, that's logged as an interactively expandable object. Not a string. Clearly not what the book says. Commented Oct 7, 2024 at 7:57
  • Either the book is grossly wrong, or, the snippet of text you've posted is out of context Commented Oct 7, 2024 at 8:01
  • 2
    "Converts ... to strings" is not a very precise wording. In a way one could say that something cannot be represented with characters unless a decision is made on which characters to render with, which sounds a lot like a conversion to string. But yes, there often is the interactive part with little expand/collapse buttons, context menus, ...etc. I wouldn't call this grossly wrong (On SO we have the funny habit to put strong adjectives everywhere: something is not just wrong, it is horribly wrong. Something is not just bad, it is horrendously bad, ...etc). Commented Oct 7, 2024 at 8:17
  • 1
    For a long time console.log did only do a simple/basic string output. And only output a single string. We used to have to use console.dir to output object details. It's moved on since then. Looks like the book was published 2020, given a few years to get into production - so written/revised around 2018. IE11 was still the major browser, so could quite possibly have been correct at the time. Commented Oct 7, 2024 at 8:52
  • @fdomn-m Oh I see! That makes sense now. It would have been wrong to just dismiss what the book said entirely. Thank you Commented Oct 7, 2024 at 9:25

1 Answer 1

1

Not in brosers

In browser consoles, objects are not converted to strings but are represented by an interactive value that you can examine and drill down to see the properties of, as well as other data that might not be immediately obvious from code alone (e.g., browser consoles usually print private fields).

const obj = {
  toString() { return "toString was called"; },
  valueOf() { return "valueOf was called"; },
  [Symbol.toPrimitive]() { return "@@toPrimitive was called"; },
}

console.log( obj )
<h1>Check the browser console</h1>

What should result from this code in most browsers is displaying the object with its methods. Rather than the object being converted to a string.

Not quite in server-side runtimes

Node, Deno, Bun, and other server-side runtimes will usually be printing objects to a text console. However, while you get a text representation of an object, it is not necessarily "converting to a string" like any other regular value will be converted.

For Node.js in particular, the conversion of an object to a printable value can be changed by defining a method using util.inspect.custom symbol, for example:

const obj = {
  [util.inspect.custom]() { 
    return "custom conversion in Node.js"; 
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Enlightening. You have given me clarification. However, does this mean that The definitive guide was wrong?
@Taofeek yes. I suspect it might have been correct at one point (version 1?) but newer editions didn't update that section.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.