The ToString () method in JavaScript

Source: Internet
Author: User
Tags local time

The ToString () method in JavaScript is used primarily for objects such as array, Boolean, Date, Error, Function, number, and so on. Here are some of the analytic and simple applications of these methods, and make a discipline to make a memo.

(1) array.tostring (): Converts the array to a string and returns the string.
Description: When the array is used in a string environment, JavaScript calls this method to automatically convert the array to a string. ToString () converts an array to a string by first converting each element of the array to a string (by invoking the ToString method of those elements). When each element is converted to a string, it outputs the strings as a list, separating the strings with commas. The return value is the same as the string returned by the Jion () method with no arguments.
Example code one:
Results: Displayed in Browser: Php,mysql,apache
Php,mysql,apache

<script type="Text/javascript">vararr = ['PHP','MySQL','Apache']; document.write ("results of using the Join method"+ arr.join () +"<br/>") document.write ("results of using the ToString method"+arr.tostring ());</script>

(2) boolean.tostring (): Converts a Boolean value to a string.
Description: Returns the string "true" or "false" based on the original Boolean value or the value of the Boolean object.
Example code two:
Result: A popup of two dialog boxes with a value of false and True

<script type="text/javascript">    varnew Boolean ();   The default value of the Boolean object is False    alert (b.tostring ());    Alert (("Chia""Chia"). toString ()); </script>

(3) date.tostring (): Converts the Date object to a string, using local time.
Example code four:
Result: Displayed in the browser:
Sat Jul 23:27:14 gmt+0800
Sat Jul 23:27:14 gmt+0800
July 19, 2008 23:27:14

(3: £ gmt+0800     from: £ gmt+0800:  :

Note: toLocaleString () is a string that returns a specification that uses the local date format in place dates.

(4) error.tostring (): Convert the Error object to a string
Description: Implements a defined string. The ECMAScript Standard provides no additional provisions except that the return value of the method is a string. In particular, it does not require that the returned word Nginx contain the error name and error information.

(5) function.tostring (): Convert function to String
Description: You can convert a function to a string in an implementation-related way. In most implementations, such as IE and Firefox, it returns the function keyword, the argument list, and the body part of the function.
Example code three:
Result: Displayed in the browser:
function Add (A, B) {var A, b, c; c = a + B; return c;}

<script type= "Text/javascript" >
function Add (A, b){
var a,b,c;
c = a + B;
return C;
}
document.write (Add.tostring ());
</script>

(6) Number.tostring (): Converts a number to a string. The cardinality or base specified with its parameters (the baseline range is 2-36). If the argument is omitted, the cardinality 10 is used. When the parameter value is 2 o'clock, a binary number is returned.
Example code four:
Result: Displayed in the browser:
34
100010
42
22

<script type="Text/javascript">varA = the; document.write (a.tostring ()+"<br/>"); document.write (A.tostring (2) +"<br/>"); document.write (A.tostring (8) +"<br/>"); document.write (A.tostring ( -) +"<br/>"); </script>

The ToString () method in JavaScript

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.