String Representations of Javascript Objects
toString() and Symbol.toStringTag The default Object.prototype.toString() function is defined to the specification to return [object X] even for non-objects:
Object.prototype.toString.call(undefined) // returns "[object Undefined]" Object.prototype.toString.call(true) // returns "[object Boolean]" {}.toString() // returns "[object Object]" X is a so-called ‘string tag’ and in newer versions of Javascipt, you can set it yourself:
{[Symbol.toStringTag]:"MyObject"}.toString() // returns "[object MyObject]" But for objects, you can also just implement the complete toString function:
{toString: ()=> "My Cool Object!