The use of ToString () and valueof () in JavaScript and the difference between the two _javascript techniques

Source: Internet
Author: User
Tags numeric value

Basically, all JS data types have both valueof and ToString methods, except NULL. Both of them solve the problem of JavaScript value operation and display.

1. The usage is as follows:

ToString () Method: Returns a string representation of an object.

Object Operation
Array Converts the elements of an Array to a string. The resulting string is separated by commas and is concatenated.
Boolean Returns "true" if the Boolean value is true. Otherwise, returns "false".
Date Returns the literal representation of a date.
Error Returns a string containing the associated error message.
Function Returns a string with the following format, where functionname is the name of the called toString method function:
function functionname( ) { [native code] }
Number Returns the text representation of a number.
String Returns the value of a String object.
Default Returns " [object objectname] ", which objectname is the name of the object type.

ValueOf () Method: Returns the original value of the specified object.

object return value
array array.tostring   and   Array.join method.
boolean boolean value.
date
function
number numeric value. The
object
string string value.

2. Common points and differences between the two:

In common: In JavaScript, the ToString () method and the ValueOf () method are invoked automatically when the object is output.

Different points: In the case of the coexistence of the two, in the numerical operation, the priority calls the valueof, the string operation, the priority invocation of ToString.

Example 1 is as follows:

<script>
 var obj = {};
 obj.valueof = function ()
 {return
 ;
 }
 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 is as follows:

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>

the difference between ****tostring vs valueof:

Returns the difference of a value type:

1. tostring must convert all content into a string

2. valueof to remove the value inside the object without type conversion

Difference of Use:

1. valueof dedicated to arithmetic and relational computing

2. ToString is dedicated to output strings

Common disadvantage: Unable to get null and undefined values

The difference between parse () and valueof (), toString () in Ps:java

1.parse () is the method inside the Simpledatefomat, and you should say parseint () or parsefloat ().

As the name implies, for example, parseint () converts a string type to an int type.

Such as

String a= "123";

int B = Integer.parseint (a);

So b is equal to 123.

The 2.ValueOf () method, such as integer.valueof (), converts a string type to an integer type (note: An integer type, not an int type, which is a simple type that represents a number. The integer type is a referenced complex type)
Such as:

String a= "123";
Integer c =integer.valueof (a);
The integer type can be converted to int type int
b =c.intvalue () with the Intvalue method;

This time, this b equals 123.

3. ToString () can convert a reference type to a string string type.

Here's an example, in contrast to 2, that converts an integer to a string type:

Integer a = new integer (123);
String b =a.tostring ();

This time, B is "123."

                        int
          parseint ()
string                       intvalue ()  
         valueof ()             
                     Integer

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.