Home >Web Front-end >JS Tutorial >Javascript converts variables into string code sharing

Javascript converts variables into string code sharing

小云云
小云云Original
2018-02-08 11:07:331406browse

Everyone should know that for JavaScript, there are 3 different ways to convert variables into strings. This article will introduce these methods in detail and compare their advantages and disadvantages. This article mainly introduces to you the three methods of converting variables into strings in Javascript. These three methods are: value.toString(), "" + value and String(value). The article introduces it in detail through sample codes. , friends who need it, please follow the editor to learn together, I hope it can help everyone.

The 3 methods to convert variables to strings are as follows:

  • value.toString()

  • "" + value

  • String(value)

When value is null or undefined, the first method will not work. And method 2 and method 3 are basically the same.

""+value: Add value to an empty string to convert it to a string. This method is actually a slightly obscure technique that may make it difficult for others to understand the developer's intentions. However, this is a matter of opinion, and some people prefer this method.

String(value): This method is very clear: use the String() function to convert value to a string. However, String() has two different uses, which is easy to confuse, especially for Java developers. When String() is used as a constructor with the new operator, it returns a newly created String object; when String() is called without the new operator, it only converts the value into the original string. The two are very different:

> String("Fundebug") === new String("Fundebug")
false
> typeof String("Fundebug")
'string'
> String("Fundebug") instanceof String
false
> typeof new String("Fundebug")
'object'
> new String("Fundebug") instanceof String
true

In fact, it's not common to use String() as a constructor, so just use it to convert strings.

""Subtle differences between +value and String(value)

""+value and String(value) can both convert value into a string. How do they do it? In fact, although their results are the same, their methods are slightly different.

Convert primitive basic type to string

Both methods use the internal function ToString() to convert primitive basic type to string. The ToString() function is defined in ECMAScript 5.1 (§9.8), but cannot be used directly, so it is called an internal function. The following table shows how the ToString() function converts primitive primitive types to strings:

Parameters Result
undefined "undefined"
null "null"
Boolean "true"or "false"
Number Convert the number to a string, for example: "1.765"
String No need to convert

Convert Object to string

Convert to character Before string, both methods will first convert Object to primitive. The difference is that ""+value uses the internal function ToPrimitive(Number) (except date type), while String(value) uses the internal function ToPrimitive(String).

  • ToPrimitive(Number): Call obj.valueOf first and return if the result is primitive; otherwise, call obj.toString() again and return if the result is primitive; otherwise return TypeError.

  • ToPrimitive(String): Similar to ToPrimitive(Number), except that obj.toString() is called first and then obj.valueOf() is called.

You can understand the difference through the following example, obj is as follows:

var obj = {
 valueOf: function()
 {
  console.log("valueOf");
  return {};
 },
 toString: function()
 {
  console.log("toString");
  return {};
 }
};

Calling results:

> "" + obj
valueOf
toString
TypeError: Cannot convert object to primitive value
> String(obj)
toString
valueOf
TypeError: Cannot convert object to primitive value

Their results are the same

" "+value is different from String(value), but we rarely feel it. Because most objects use the default valueOf(), which returns the object itself:

> var x = {}
> x.valueOf() === x
true

Since the return value of valueOf() is not primitive, ToPrimitive(Number) will skip valueOf() and return toString() The return value. In this way, the return value of ToPrimitive(String) is the same.

When object is a Boolean, Number or String instance, valueOf() will return primitive. This means that the calculation process of the two is like this:

  • ToPrimitive(Number): valueOf() returns the primitive value, and then uses ToString() to convert it to a string.

  • ToPrimitive(String) : toString() converts the primitive value into a string through the ToString() function.

It can be seen that although the calculation process is different, their results are the same.

Conclusion

So which method should you choose? If you can ensure that the value is not null or undefined, then use value.toString(). Otherwise, you can only use ""+value and String(value), which are basically the same.

Related recommendations:

JavaScript method to convert a Date object into a string based on local time toLocaleString()

Javascript Introduction to the method of converting variables into strings

php Converting time differences into string prompts for detailed examples

The above is the detailed content of Javascript converts variables into string code sharing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn