• Skip to main content
  • Skip to search
  • Skip to select language
HTML

Structure of content on the web

  • Web APIs

    Interfaces for building web applications

  • Learn
    • CSS

      Learn to style content using CSS

    • Overview

      A customized MDN experience

    • FAQ

      Frequently asked questions about MDN Plus

  • HTTP Observatory

    Scan a website for free

  • JavaScript
  • toString()
      • Deutsch
      • Español
      • Français
      • 日本語
      • 한국어
      • Português (do Brasil)
      • Русский
      • 中文 (简体)

    In this article

    • Try it
    • Syntax
    • Description
    • Examples
    • Specifications
    • Browser compatibility
    • See also
    1. assign()
    2. entries()
    3. getOwnPropertyDescriptors()
    4. groupBy()
    5. isFrozen()
    6. seal()
    7. __defineGetter__() Deprecated
    8. __defineSetter__() Deprecated
    9. __lookupGetter__() Deprecated
    10. __lookupSetter__() Deprecated
    11. toLocaleString()
    12. __proto__ Deprecated
    13. bind()
    14. displayName Non-standard
    15. arguments Non-standard Deprecated
    16. caller Non-standard Deprecated
  • Instance methods
    1. __defineGetter__() Deprecated
    2. __defineSetter__() Deprecated
    3. __lookupGetter__() Deprecated
    4. __lookupSetter__() Deprecated
    5. toLocaleString()
    6. __proto__ Deprecated
    7. type coercion logic.

  • Try it

    const map = new Map();
    
    console.log(map.toString());
    / Expected output: "[object Map]"
    

    Syntax

    js
    toString()
    

    Parameters

    By default toString() takes no parameters. However, objects that inherit from Object may override it with their own implementations that do take parameters. For example, the convert an object to a primitive value. You rarely need to invoke the toString method yourself; JavaScript automatically invokes it when encountering an object where a primitive value is expected.

    This method is called in priority by valueOf() method returns an object, the toString() method is usually called in the end, unless the object overrides valueOf(). For example, +[1] returns 1, because its toString() method returns "1", which is then converted to a number.

    All objects that inherit from Object.prototype (that is, all except [Symbol.toPrimitive]() method, which allows even more control over the conversion process, and will always be preferred over valueOf or toString for any type conversion.

    To use the base Object.prototype.toString() with an object that has it overridden (or to invoke it on null or undefined), you need to call Function.prototype.apply() on it, passing the object you want to inspect as the first parameter (called thisArg).

    js
    const arr = [1, 2, 3];
    
    arr.toString(); / "1,2,3"
    Object.prototype.toString.call(arr); / "[object Array]"
    

    Object.prototype.toString() returns "[object Type]", where Type is the object type. If the object has a Symbol, have a Symbol.toStringTag. Some objects predating ES6 do not have Symbol.toStringTag, but have a special tag nonetheless. They include (the tag is the same as the type name given below):

    • Array
    • typeof returns "function")
    • Error
    • Boolean
    • Number
    • String
    • Date
    • RegExp

    The arguments object returns "[object Arguments]". Everything else, including user-defined classes, unless with a custom Symbol.toStringTag, will return "[object Object]".

    Object.prototype.toString() invoked on valueOf(), is used instead, or a TypeError is thrown if none of these methods return a primitive.

    The following code defines a Dog class.

    js
    class Dog {
      constructor(name, breed, color, sex) {
        this.name = name;
        this.breed = breed;
        this.color = color;
        this.sex = sex;
      }
    }
    

    If you call the toString() method, either explicitly or implicitly, on an instance of Dog, it returns the default value inherited from Object:

    js
    const theDog = new Dog("Gabby", "Lab", "chocolate", "female");
    
    theDog.toString(); / "[object Object]"
    `${theDog}`; / "[object Object]"
    

    The following code overrides the default toString() method. This method generates a string containing the name, breed, color, and sex of the object.

    js
    class Dog {
      constructor(name, breed, color, sex) {
        this.name = name;
        this.breed = breed;
        this.color = color;
        this.sex = sex;
      }
      toString() {
        return `Dog ${this.name} is a ${this.sex} ${this.color} ${this.breed}`;
      }
    }
    

    With the preceding code in place, any time an instance of Dog is used in a string context, JavaScript automatically calls the toString() method.

    js
    const theDog = new Dog("Gabby", "Lab", "chocolate", "female");
    
    `${theDog}`; / "Dog Gabby is a female chocolate Lab"
    

    Using toString() to detect object class

    toString() can be used with every object and (by default) allows you to get its class.

    js
    const toString = Object.prototype.toString;
    
    toString.call(new Date()); / [object Date]
    toString.call(new String()); / [object String]
    / Math has its Symbol.toStringTag
    toString.call(Math); / [object Math]
    
    toString.call(undefined); / [object Undefined]
    toString.call(null); / [object Null]
    

    Using toString() in this way is unreliable; objects can change the behavior of Object.prototype.toString() by defining a Symbol.toStringTag property, leading to unexpected results. For example:

    js
    const myDate = new Date();
    Object.prototype.toString.call(myDate); / [object Date]
    
    myDate[Symbol.toStringTag] = "myDate";
    Object.prototype.toString.call(myDate); / [object myDate]
    
    Date.prototype[Symbol.toStringTag] = "prototype polluted";
    Object.prototype.toString.call(new Date()); / [object prototype polluted]
    

    Specifications

    Specification
    ECMAScript® 2026 Language Specification
    # sec-object.prototype.tostring

    Browser compatibility

    See also

    • Polyfill of Object.prototype.toString with Symbol.toStringTag support in core-js
    • Object.prototype.valueOf()
    • Number.prototype.toString()
    • Symbol.toPrimitive
    • Symbol.toStringTag

    Help improve MDN

    Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

    Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant