Skip to content


Why Object.toString ?

Each object has a toString () method that is automatically called when the object is represented as a text value, or when an object is sent as a string is expected. By default, the toString () method is inherited by each object from Object. If this method is not overridden in a custom object, toString () returns the value of “[object type]“; where type is the object type.

The toString( ) method is not one you often call explicitly in your JavaScript programs. Instead, you define this method in your objects, and the system calls it whenever it needs to convert your object to a string.

The JavaScript system invokes the toString( ) method to convert an object to a string whenever the object is used in a string context.



  • obj: A string representing the object.[])
// “[object Array]”{})
// “[object Function]”{})
// “[object Object]”
// “[object Null]”
// “[object Undefined]”
// “[object Number]”
// “[object Number]”
// “[object Number]”
// “[object Number]”
// “[object Boolean]””)
// “[object String]”
// “[object RegExp]”
// “[object Undefined]” 0)
// “[object Undefined]”
// “[object global]”
// “[object HTMLDocument]”
// “[object Location]”
// “[object String]”
// “[object Function]”
// “[object Object]”
// “[object Function]”
// “[object Function]”
// “[object String]” Date())
// “[object Date]”
// “[object Math]”
// “[object Function]”
// “[object Symbol]”
// “[object JSON]”

JavaScript Object.toString Examples

Example 1

var o = {a:1, b:2}
console.log('Item: ' + o)


{a: 1, b: 2}
[object Object]

Example 2

        function Dog(name) {
   = name;

        dog1 = new Dog('Gabby');

        Dog.prototype.toString = function dogToString() {





ECMAScript 2015 added this note:

NOTE Historically, this function was occasionally used to access the String value of the [[Class]] internal slot that was used in previous editions of this specification as a nominal type tag for various built-in objects. The above definition of toString preserves compatibility for legacy code that uses toString as a test for those specific kinds of built-in objects. It does not provide a reliable type testing mechanism for other kinds of built-in or program defined objects. In addition, programs can use @@toStringTag in ways that will invalidate the reliability of such legacy type tests.

ECMAScript 2015 §Fundamental Objects#sec-object.prototype.tostring

Browser Support

Internet Exploreryes
Android webviewyes
Chrome for Androidyes
Edge mobileyes
Firefox for Android4
Opera Androidyes

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: