A detailed description of the JavaScript toString () function

Source: Internet
Author: User
Tags object object

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.