Skip to content

Object.toString()

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.

Syntax:

obj.toString()

  • obj: A string representing the object.

Object.prototype.toString.call([])
// “[object Array]”
Object.prototype.toString.call(function(){})
// “[object Function]”
Object.prototype.toString.call({})
// “[object Object]”
Object.prototype.toString.call(null)
// “[object Null]”
Object.prototype.toString.call(undefined)
// “[object Undefined]”
Object.prototype.toString.call(1)
// “[object Number]”
Object.prototype.toString.call(1.1)
// “[object Number]”
Object.prototype.toString.call(NaN)
// “[object Number]”
Object.prototype.toString.call(Infinity)
// “[object Number]”
Object.prototype.toString.call(true)
// “[object Boolean]”
Object.prototype.toString.call(”)
// “[object String]”
Object.prototype.toString.call(/./g)
// “[object RegExp]”
Object.prototype.toString.call()
// “[object Undefined]”
Object.prototype.toString.call(void 0)
// “[object Undefined]”
Object.prototype.toString.call(window)
// “[object global]”
Object.prototype.toString.call(document)
// “[object HTMLDocument]”
Object.prototype.toString.call(document.location)
// “[object Location]”
Object.prototype.toString.call(document.location.href)
// “[object String]”
Object.prototype.toString.call(Object)
// “[object Function]”
Object.prototype.toString.call(Object.prototype)
// “[object Object]”
Object.prototype.toString.call(Object.prototype.toString)
// “[object Function]”
Object.prototype.toString.call(Date)
// “[object Function]”
Object.prototype.toString.call(Date())
// “[object String]”
Object.prototype.toString.call(new Date())
// “[object Date]”
Object.prototype.toString.call(Math)
// “[object Math]”
Object.prototype.toString.call(Math.abs)
// “[object Function]”
Object.prototype.toString.call(Symbol())
// “[object Symbol]”
Object.prototype.toString.call(JSON)
// “[object JSON]”


JavaScript Object.toString Examples

Example 1

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

output:

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


Example 2

        function Dog(name) {
            this.name = name;
        }

        dog1 = new Dog('Gabby');

        Dog.prototype.toString = function dogToString() {
            return this.name;
        }

        console.log(dog1.toString());

output:

Gabby


Sources:


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

Chromeyes
Edgeyes
Firefox1
Internet Exploreryes
Operayes
Safariyes
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:

WordPress.com Logo

You are commenting using your WordPress.com 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: