Basically, all JS data types have the valueOf and toString methods, except null. These two solutions solve the problem of javascript value calculation and display. This article introduces the usage of tostring () and valueof () in javascript and the difference between them, if you are interested in jstostringvalueof, learn about it. Basically, all JS data types have the valueOf and toString methods, except null. They solve the javascript value calculation and display problems.
1. Usage:
ToString () method: returns the string representation of an object.
Object |
Operation |
Array |
SetArrayTo the string. The result string is separated by commas and connected. |
Boolean |
If the Boolean value isTrueReturns "true ". Otherwise, "false" is returned ". |
Date |
Returns the text representation of a date. |
Error |
Returns a string containing the relevant error message. |
Function |
Returns a string in the following format:FunctionnameYesToStringMethod Function Name:function functionname( ) { [native code] } |
Number |
Returns the text representation of a number. |
String |
ReturnStringObject value. |
Default |
Return"[object objectname] ", Whereobjectname Is the name of the object type. |
ValueOf () method: returns the original value of the specified object.
Object |
Return Value |
Array |
Elements of the array are converted into strings separated by commas (,) and connected together. Its operation andArray. toStringAndArray. joinThe method is the same. |
Boolean |
Boolean value. |
Date |
The storage time is the number of milliseconds from midnight, January 1, January 1, 1970 in UTC. |
Function |
Function itself. |
Number |
Numeric value. |
Object |
Object. This is the default situation. |
String |
String value. |
2. similarities and differences between the two:
In JavaScript, The toString () and valueOf () methods are automatically called when an object is output.
Difference: in the case of coexistence of the two, valueOf is preferentially called in numerical operations, and toString is preferentially called in string operations.
Example 1 is as follows:
《script》 var obj = {}; obj.valueOf = function() { return 10; } obj.toString = function() { return "return value"; } var result = obj + 1; //var result = obj.valueOf() + 1; alert(result); alert(obj); //alert(obj.toString());《script》
Example 2:
function obj() { } obj.prototype.toString = function(){ return 'dfsf'; }; obj.prototype.valueOf = function(){ return '3333'; }; var e = new obj(); var o = new obj(); alert(o);//alert(obj.toString()) alert(o+e);//alert(obj.valueOf()+obj.valueOf())《script》
* *** Difference between toString vs valueOf:
Differences in return value types:
1. toString must convert all content into a string
2. valueOf extracts the internal values of the object without type conversion.
Usage differences:
1. valueOf is used for arithmetic calculation and relational calculation.
2. toString is used to output strings.
Common disadvantage: the null and undefined values cannot be obtained.
PS: differences between parse (), valueOf (), and toString () in Java
1. parse () is the method in SimpleDateFomat. You should be talking about parseInt () or parsefloat,
For example, parseInt () converts String type to int type.
For example
String a = "123 ";
Int B = Integer. parseInt ();
In this way, B equals 123.
2. valueOf () method such as Integer. valueOf () is to convert the String type to the Integer type (Note: it is an Integer type, not an int type, int type is a simple type that represents a number, and Integer type is a reference complex type)
For example:
String a = "123"; Integer c = Integer. valueOf (a); // The Integer type can be converted to int type int B = c. intValue () using the intValue method ();
At this time, B is equal to 123.
3. toString () can convert a reference type to a String type.
In the following example, the Integer type is converted to the String type:
Integer a = new Integer (123 );
String B = a. toString ();
At this time, B is 123.
Int
ParseInt ()
String intValue ()
ValueOf ()
Integer