Returns a string representing the specified object.
Object
toString() toString(radix)
radix
(Optional) An integer between 2 and 16 specifying the base to use for representing numeric values.
Every object has a toString method that is automatically called when it is to be represented as a text value or when an object is referred to in a string concatenation. For example, the following examples require theDog to be represented as a string:
You can use toString within your own code to convert an object into a string, and you can create your own function to be called in place of the default toString method.
Every object type has a built-in toString method, which JavaScript calls whenever it needs to convert an object to a string. If an object has no string value and no user-defined toString method, toString returns [object type], where type is the object type or the name of the constructor function that created the object.
Some built-in classes have special definitions for their toString methods. See the descriptions of this method for these objects:
You can create a function to be called in place of the default toString method. The toString method takes no arguments and should return a string. The toString method you create can be any value you want, but it will be most useful if it carries information about the object.
The following code defines the Dog object type and creates theDog, an object of type Dog:
function Dog(name,breed,color,sex) { this.name=name this.breed=breed this.color=color this.sex=sex } theDog = new Dog("Gabby","Lab","chocolate","girl")
The following code creates dogToString, the function that will be used in place of the default toString method. This function generates a string containing each property, of the form property = value;.
function dogToString() { var ret = "Dog " + this.name + " is [" for (var prop in this) ret += " " + prop + " is " + this[prop] + ";" return ret + "]" }
The following code assigns the user-defined function to the object's toString method:
Dog.prototype.toString = dogToString
With the preceding code in place, any time theDog is used in a string context, JavaScript automatically calls the dogToString function, which returns the following string:
Dog Gabby is [ name is Gabby; breed is Lab; color is chocolate; sex is girl; toString is function dogToString() { var ret = "Object " + this.name + " is ["; for (var prop in this) { ret += " " + prop + " is " + this[prop] + ";"; } return ret + "]"; } ;]
An object's toString method is usually invoked by JavaScript, but you can invoke it yourself as follows:
alert(theDog.toString())
The following example prints the string equivalents of the numbers 0 through 9 in decimal and binary.
for (x = 0; x < 10; x++) { ("Decimal: ", x.toString(10), " Binary: ", Console.write x.toString(2)) }
The preceding example produces the following output:
Decimal: 0 Binary: 0 Decimal: 1 Binary: 1 Decimal: 2 Binary: 10 Decimal: 3 Binary: 11 Decimal: 4 Binary: 100 Decimal: 5 Binary: 101 Decimal: 6 Binary: 110 Decimal: 7 Binary: 111 Decimal: 8 Binary: 1000 Decimal: 9 Binary: 1001
Object.valueOf