JavaScript » Object » toString

Syntax:
Object.toString()

The toString method returns a string representing a specified object.

The toString method is inherited by every object descended from Object and returns a string representing a specified object. There are times when an object needs to be represented as a string, and the toString method (which comes with every object) is automatically called to do that. ToString returns the object type or the constructor function that created it.
 
The toString method can, however, be overwritten in a custom object by assigning a user-defined function in its place as follows:

NOTE:
 
Every core JavaScript object will over-ride the toString method to return an appropriate value, and will only call it when it needs to convert an object to a string.

Examples

Code:
Cat.prototype.toString = myToString
Explanation:

The toString method can be overwritten in a custom object by assigning a user-defined function in its place.

Code:
document.write(Sheeba)
Output:
[object Object]
Code:
document.write(Sheeba.toString)
Output:
function toString() { [native code] }