Console.log confusion

If I console a number, does console.log method coerce that number value to a string to print it out?

We’ll, it has to, otherwise how would you be able to see it? Console log prints strings to the console

Console.log in browsers is fairly magical, since it’ll turn objects into clicky tree widgets and such. But in node, I think it just stringifies it, so all you get is ‘[Object]’. Numbers will print without quotes, so strictly speaking it’s not just stringifying the data. Something like python’s __repr__, except more or less built-in.

At least I think so, I’m on my gaming machine right now which doesn’t have npm or node. Let’s see if I can get a hat trick of wrong assumptions today :smile:

I was maybe being a bit literal/reductive: it definitely does convert to a string, but only to print those strings as lines to the console. And those lines may well go through another process first (like Firefox uses React, so I maybe it’s easier to deal with the data in non-stringified form as it goes into React? But maybe not, cos that data isn’t really interactive in any way beyond pretty printing)

I’m probably just stating the obvious here. I just want to make sure the OP is not asking about the value coercion of numbers before they get printed to the console. It does it for printing to the console, not before. It also shows the type by color-coding the return value (numbers are purple in Chrome).

console.log(typeof (2+2))
// number
1 Like