Does console.log invokes toString method of an object?

Viewed 19649

As per this documentation,

The string representations of each of these objects are appended together in the order listed and output.

Also as per answer

The + x coerces the object x into a string, which is just [object Object]:

So, my question is

If I do

str = new String("hello")
console.log(str) //prints the string object but not 'hello'
console.log(""+str) //prints "hello"

So, in first case, it simply prints the object (doesn't invoke the toString() method).

But in second case, it doesn't coerce but simply print the primitive value. Why is that so?

Which method does console.log invokes to print the object?

Please note that - this is not a duplicate of this question.

4 Answers

This is more typing but will invoke obj.toString() as well:

console.log(`${obj}`);
Related