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 |
var arr = [ ‘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 |
var b = new Boolean(); //boolean对象默认值为false |
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.
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.
6 |
document.write(add.toString()); |
Program Run Result:
1 |
function add(a,b){ var a,b,c; c = a + b; return c; } |
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.
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:
Extended Reading
The list of topics for this article is as follows:
- JavaScript function Completion: Splice () array operation
- JavaScript function Completion: sort () sorting
- JavaScript function Completion: toString ()
JavaScript function Completion: toString ()