Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does console.log invokes toString method of an object?

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.

like image 428
gurvinder372 Avatar asked Mar 25 '16 06:03

gurvinder372


People also ask

What does console log () do?

The console. log() is a function in JavaScript which is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user. Syntax: console. log(A);

Does object have a toString method?

toString() method returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read.

In which of the following is toString () method defined?

toString() is defined in java. lang. Object. 19. compareTo() returns.

How do you console log an object?

log(JSON. stringify(obj)) method can be useful for logging the object to the console as string, as long as the data in the object is JSON-safe. For complex objects, the method Object. entries(obj) is a way of looping through an object that can be used to log the object to the console.


1 Answers

Console API is not a standard API that is defined in any specification but is something that is implemented across all browsers, so vendors are usually at their liberty to implement in their own fashion as there's no standard spec to define the output of any methods in API.

Unless you check the actual implementation of the Console API for a particular browser, you can never be sure. There's a tracker on GitHub listing the differences between implementation from major browsers.

If you look at the implementation in FF (available here - search for log), it has a comment below

A multi line stringification of an object, designed for use by humans

The actual implementation checks for the type of argument that is passed to log() and based on it's type, it generates a different representation.

Coming to your case, log() prints two different values for strings created using literal notation and strings created using String constructor because they are two different types. As explained here, Strings created using literal notation are called String Primitives and strings created using String constructor are called String Objects.

var str1 = 'test'; var str2 = new String('hello');  typeof str1 // prints "string" typeof str2 // prints "object" 

As the types differ, their string representation differs in the Console API. If you go through the code for FF's Console implementation, the last statement is

return "  " + aThing.toString() + "\n"; 

So to answer your question, Console API in FF calls toString() on the argument only if the argument type is not one of {undefined,null,object,set,map} types. It doesn't always call toString() or valueOf() methods. I didn't check the implementation of Chrome, so I won't comment on that.

like image 67
Arkantos Avatar answered Sep 20 '22 10:09

Arkantos