Skip to content
Related Articles

Related Articles

Improve Article
JavaScript Object.prototype.toString() Method
  • Last Updated : 23 Apr, 2021

In JavaScript, the  Object.prototype.toString() method is used to return a string that can represent the object. The toString() method is automatically inherited by every object which is inherited from Object.  Whenever an object is to represented as a text value or a string is expected from the object, the toString() method is called automatically.  

Syntax:

obj.toString()

If one does not override the toString() method in case of custom objects, the toString() method returns the following:

[object type] 

In the above syntax, the type denotes the object type.

Another use of the toString() method is that it can be used to convert base 10 numbers (and even bigInts) to other base numbers. 

 



Syntax:

ExNum.toString(radix);

In the above syntax, the ExNum is an object of the object type number or bigInt and the radix refers to the base the number to is be converted to.

Example 1: The following example shows how the toString() method works when dealing with default object type and custom object type when the toString() method is not overridden.

JaaScript




<script>
  
    // Creating a custom object type
    function ExObjType(n) {
        this.number = n;
    }
  
    function myFunction() {
      
        // Creating an object of the
        // String class
        const o = new String('Hello');
        console.log(o.toString());
  
        // Creating an object ExObj1 of
        // custom defined class  
        const ExObj1 = new ExObjType(3);
  
        // Returns [object Object] as toString()
        // not overridden for custom class
        console.log(ExObj1.toString());
    }
    myFunction();
</script>

Output:

Example 2: The following example shows how the toString() method works when dealing with custom object type and the toString() method is overriden  :

JavaScript




<script>
  
    // Creating a custom object type
    function ExObjType(n) {
        this.number = n;
    }
  
    // Overriding the toString() method
    // for ExObjectType
    ExObjType.prototype.toString = function ExObjToString() {
        const ans = 'The number related to this object is '
                + this.number;
        return ans;
    }
  
    function myFunction() {
  
        // Creating an object ExObj1 of 
        // custom defined class  
        const ExObj1 = new ExObjType(3);
  
        // Calling the toString() method
        // after overriding
        console.log(ExObj1.toString());
    }
    myFunction();
</script>

Output:

Example 3: The following example shows how to use the toString() method to convert base 10 numbers to different base numbers.

JavaScript




<script>
    function myFunction() {
  
        // Creating a constant num1
        // with value 12
        const num1 = 12;
  
        // Converts num1 to a binary form
        console.log(num1.toString(2));
  
        // Converts num1 to its octal form
        console.log(num1.toString(8));
          
        // Converts num1 to its base-5 form
        console.log(num1.toString(5));
    }
    myFunction();
</script>

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :