The use of tostring of and valueof of in javascript and the difference between the two

  • 2020-11-03 22:01:22
  • OfStack

Basically, all JS data types have both valueOf and toString methods, with the exception of null. Both of them solve the problem of javascript value operation and display.

1. Usage:

The toString() method returns a string representation of the object.

对象 操作
Array 将 Array 的元素转换为字符串。结果字符串由逗号分隔,且连接起来。
Boolean 如果 Boolean 值是 true,则返回 “true”。否则,返回 “false”。
Date 返回日期的文字表示法。
Error 返回1个包含相关错误消息的字符串。
Function 返回如下格式的字符串,其中 functionname 是被调用 toString 方法函数的名称:

CODE_TAG_REPLACE_MARK_0
Number 返回数字的文字表示。
String 返回 String 对象的值。
默认 返回 “ CODE_TAG_REPLACE_MARK_1 ”,其中  CODE_TAG_REPLACE_MARK_2  是对象类型的名称。

The valueOf() method returns the original value of the specified object.

对象 返回值
Array 数组的元素被转换为字符串,这些字符串由逗号分隔,连接在1起。其操作与 Array.toString 和 Array.join方法相同。
Boolean Boolean 值。
Date 存储的时间是从 1970 年 1 月 1 日午夜开始计的毫秒数 UTC
Function 函数本身。
Number 数字值。
Object 对象本身。这是默认情况。
String 字符串值。

2. Similarities and Differences between the two:

Common ground: In JavaScript, the toString() and valueOf() methods are called automatically when the object is output.

Difference: In the case of the coexistence of the two, valueOf is called first in the numerical operation, and toString is called first in the string operation.

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

****toString vs valueOf Differences:

Differences in return value types:

1. toString1 will turn everything into a string

2. valueOf fetches the value inside the object without type conversion

Differences in use:

1. valueOf is dedicated to arithmetic and relational operations

2. toString is dedicated to output strings

Common disadvantage: Cannot get the value of null and undefined

PS: The difference between parse () and valueOf (), toString () in Java

1.parse() is the method in SimpleDateFomat. You should say parseInt() or parsefloat().

As the name implies for example, parseInt() converts the String type to int type.

Such as

String a= "123";

int b = Integer.parseInt(a);

So b is equal to 123.

2.ValueOf() methods such as ES101en. valueOf() convert String type to Integer type (note: Integer type, not int type, int is a simple type for Numbers, Integer is a complex type for 1 reference)
Such as:


String a= "123";
Integer c =Integer.valueOf(a);
//Integer The type can be used intValue The method is converted to int type 
int b =c.intValue();

So b is equal to 123

3. toString() converts 1 reference type to String string type.

Here's an example of converting Integer to String as opposed to 2:

Integer a = new Integer(123);
String b =a.toString();

So b is 123

int
parseInt()
String intValue()
ValueOf()
Integer


Related articles: