Skip to content
Tutorials Center
My learning

Topics

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.

JavaScript
String({});                              // "[object Object]"
Object.prototype.toString.call([]);      // "[object Array]"

Try it

JS The default string and overrides

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Type tags

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
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. undefined gives Undefined and null gives Null. Otherwise the engine uses the object's Symbol.toStringTag if it is a string, falling back to built-in tags such as Array, Function, Error, Boolean, Number, String, Date, RegExp, or Object.
  • Type checking with .call(). Object.prototype.toString.call(value) works on any value, including null, and distinguishes arrays, dates and regular expressions. Today, Array.isArray() and instanceof are often clearer.
  • Customize with Symbol.toStringTag to label your own classes, as the second example shows.
  • Override toString() to control how your objects appear in strings. Conversion to string first uses Symbol.toPrimitive if present, then toString(), then valueOf().
  • Not for display of data. "[object Object]" in a UI almost always means you forgot to access a property or use JSON.stringify().
  • Spoofable. Because Symbol.toStringTag can be set freely, the tag is a hint, not a security check.
  • Other built-in overrides (such as Number.prototype.toString(radix) and Array.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.