toString()
function is used to
returns the current object as a string。
This method belongs Object
to an object, and since all objects have "inherited" an object instance, this method can be used by almost all instance objects.
This function is supported by all major browsers.
Grammar
Object. ToString()
return value
toString()
The return value of the function is of type string. Returns the string form of the current object.
Many of the built-in objects of JavaScript rewrite the function to make it more suitable for its own functional needs.
type |
Behavior Description |
Array |
Converts each element of the Array to a string and joins them in turn, stitching together two elements with commas as delimiters. |
Boolean |
Returns "true" if the Boolean value is true. Otherwise, "false" is returned. |
Date |
Returns the text representation of the date. |
Error |
Returns a string containing the associated error information. |
Function |
Returns a string of the following format, where functionname is the name of a function, and the toString method of this function is called: "function functionname () {[native code]}" |
Number |
Returns a string representation of a numeric value. You can also return a string specified as a binary representation, refer to number.tostring (). |
String |
Returns the value of a String object. |
Object (default) |
Returns "[Object ObjectName]", where ObjectName is the name of the object type. |
Example & Description
The following example runs the host environment for the Windows 7 Simplified Chinese flagship 64-bit, located in mainland China. Different locales and language settings may result in different output results.
Array
VarArray= ["Codeplayer", True, 12, -5];
Document.Writeln(Array.Tostring() ); Codeplayer,true,12,-5
Date
VarDate= New Date(2013, 7, 18, 23, 11, 59, 230);
Document.Writeln(Date.Tostring() ); Sun 23:11:59 gmt+0800 (China Standard Time)
Date 2
VarDate2= New Date(1099, 7, 18, 23, 11, 59, 230);
Document.Writeln(Date2.Tostring() ); Fri 1099 23:11:59 gmt+0800 (China Standard Time)
Digital
VarNum=15.26540;
Document.Writeln(Num.Tostring() ); 15.2654
Boolean
Varbool= True;
Document.Writeln(bool.Tostring() ); True
Object
VarObj= {Name: "Zhang San",Age: 18};
Document.Writeln(Obj.Tostring() ); [Object Object]
HTML DOM Node
VarEles= Document. "body" Document. Writeln ( Eles. Tostring ); //[object Nodelist]
Document. Writeln ( Eles[0].< Span class= "PLN" >tostring () ); //[object Htmlbodyelement]
Run code
A detailed description of the JavaScript toString () function