Question about tostring() of an object

According to MDN ,

By default, the toString() method is inherited by every object descended from Object . If this method is not overridden in a custom object, toString() returns " [object type] ", where type is the object type.

Consider the following example

const toString = Object.prototype.toString;

toString.call(new Date);    // [object Date]
toString.call(new String);  // [object String]
toString.call(Math);        // [object Math]

I don’t understand why toString.call(new String) returns [object String]
From the MDN, it should return [object type] where type should be object in this case
because console.log (new String) print out {" " } , it is an object
the same goes for new Date, it also displaced “object” after typing typeof new Date

Yeah, I hadn’t noticed that before. I think that here mentions:

Object : typeof instance === "object" . Special non-data but structural type for any constructed object instance also used as data structures: new Object, new Array, new Map, new Set, new WeakMap, new WeakSet, new Date and almost everything made with new keyword;

So, in this. case it is the "structural type ".

1 Like