Home  >  Article  >  Web Front-end  >  How to convert javascript variables to strings

How to convert javascript variables to strings

青灯夜游
青灯夜游Original
2021-06-18 15:30:196076browse

Methods to convert js variables to strings: 1. Use the "value.toString()" statement. toString() can convert js values ​​(except null and undefined) into strings; 2. Use "" "value" statement; 3. Use the "String(value)" statement.

How to convert javascript variables to strings

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

The 3 methods of javascript converting variables into 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: Convert value to a string by adding it to the empty 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 together with operator new, it returns a newly created String object; when String() is called without new operator, it only value is converted into a raw 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, using String() as a constructor is uncommon, so just use it to convert strings.

<span style="color: #ff0000"><strong>“”Subtle difference between value</strong></span> and String(value)

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

Convert the primitive basic type to a string

Both methods use the internal functionToString()Convert the primitive Basic types are converted to strings. ToString()The 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:

##null"null"Boolean"true" or "false"NumberConvert a number to a string, For example: "1.765"##String

将Object转换为字符串

转换为字符串之前,两种方法都会先将Object转换为primitive。不同的是,""+value使用内部函数ToPrimitive(Number) (除了date类型),而String(value)使用内部函数ToPrimitive(String)

  • ToPrimitive(Number) : 先调用obj.valueOf ,若结果为primitive则返回;否则再调用obj.toString() ,若结果为primitive则返回;否则返回TypeError。
  • ToPrimitive(String) : 与ToPrimitive(Number)类似,只是先调用obj.toString() ,后调用obj.valueOf()

可以通过以下示例了解区别,obj如下:

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

调用结果:

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

它们的结果相同

""+valueString(value)虽然不同,但是我们很少能感觉到。因为,大多数object使用默认的valueOf() ,它返回对象本身:

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

由于valueOf()返回值并非primitive,因此ToPrimitive(Number)会跳过valueOf() ,而返回toString()的返回值。这样,与ToPrimitive(String)的返回值就一样了。

当object是Boolean、Number或者String实例时,valueOf()将返回primitive。这就意味着两者的计算过程是这样的:

  • ToPrimitive(Number) valueOf()返回primitive值,然后使用ToString()转换为字符串。
  • ToPrimitive(String) : toString()通过ToString()函数将primitive值转换为字符串。

可知,虽然计算过程不同,但是它们的结果是一样的。

结论

那么你该选择哪种方法呢?如果你可以确保value值不是null和undefined,那么不妨使用value.toString() 。否则,你只能使用""+valueString(value) ,它们基本上是一样的。

【相关推荐:javascript学习教程

Parameters Result
undefined "undefined"
No need to convert

The above is the detailed content of How to convert javascript variables to strings. 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