JavaScript Reference ยท Object
Object.prototype.toString()
Reference for JavaScript Object.prototype.toString(): the default [object Type] string, Symbol.toStringTag, type checks and how to override it.
Every object inherits toString() from Object.prototype. Called with no changes, it returns a string of the form [object Tag], where the tag names the kind of object. Most built-in types override toString() with something more useful (arrays join their items, functions return their source), but the original is still available and gives a reliable type label.
Template literals, string concatenation and many APIs call toString() automatically when an object must become text.
String({}); // "[object Object]" Object.prototype.toString.call([]); // "[object Array]"
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
obj.toString() Object.prototype.toString.call(value)
This method takes no parameters.
Return value: a string. For the default implementation it is "[object " + tag + "]".
Exceptions: none for the default method. Calling it directly on a null-prototype object fails because the method is missing.
Usage notes
- How the tag is chosen.
undefinedgivesUndefinedandnullgivesNull. Otherwise the engine uses the object'sSymbol.toStringTagif it is a string, falling back to built-in tags such asArray,Function,Error,Boolean,Number,String,Date,RegExp, orObject. - Type checking with
.call().Object.prototype.toString.call(value)works on any value, includingnull, and distinguishes arrays, dates and regular expressions. Today,Array.isArray()andinstanceofare often clearer. - Customize with
Symbol.toStringTagto label your own classes, as the second example shows. - Override
toString()to control how your objects appear in strings. Conversion to string first usesSymbol.toPrimitiveif present, thentoString(), thenvalueOf(). - Not for display of data.
"[object Object]"in a UI almost always means you forgot to access a property or useJSON.stringify(). - Spoofable. Because
Symbol.toStringTagcan be set freely, the tag is a hint, not a security check. - Other built-in overrides (such as
Number.prototype.toString(radix)andArray.prototype.toString()) behave differently, so look at the specific type.
Browser support
Object.prototype.toString() is available in every browser and JavaScript runtime. Symbol.toStringTag is supported by all current browsers.