JavaScript function Completion: toString ()

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.

Array.tostring ()

Converts an 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.

1 vararr = [‘php‘,‘mysql‘,‘apache‘];
2 document.write("使用join方法的结果"+ arr.join() + "<br />")
3 document.write("使用toString方法的结果"+ arr.toString());

Program Run Result:

1 使用join方法的结果php,mysql,apache
2 使用toString方法的结果php,mysql,apache
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.

1 varb = newBoolean();//boolean对象默认值为false
2 alert(b.toString());
3 alert(("chia"== "chia").toString());

Result: A popup of two dialog boxes with a value of false and true.

Date.tostring ()

Converts a Date object to a string, using local time.

1 vartoday = newDate();
2 document.write(today + "<br />");
3 document.write(today.toString() + "<br />");
4 document.write(today.toLocaleString());

Results:

Tue Feb 11:35:21 gmt+0800 (China Standard Time)
Tue Feb 11:35:21 gmt+0800 (China Standard Time)
2016/2/23 11:35:21

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

Error.tostring ()

Converts 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.

Function.tostring ()

Converts the function into a 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.

1 functionadd(a,b){
2     vara,b,c;
3     c = a + b;
4     returnc;
5 }
6 document.write(add.toString()); 

Program Run Result:

1 functionadd(a,b){ var a,b,c; c = a + b; returnc; }
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.

1 vara = 34;
2 document.write(a.toString() + "<br />");
3 document.write(a.toString(2) + "<br />");
4 document.write(a.toString(8) + "<br />");
5 document.write(a.toString(16) + "<br />");

Program Run Result:

1 34
2 100010
3 42
4 22
Extended Reading

The list of topics for this article is as follows:

      1. JavaScript function Completion: Splice () array operation
      2. JavaScript function Completion: sort () sorting
      3. JavaScript function Completion: toString ()

JavaScript function Completion: toString ()

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.