Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does javascript convert a variable to a string

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "javascript how to convert variables into strings", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "javascript how to convert variables into strings" bar!

Js variable into a string method: 1, using the "value.toString ()" statement, toString () can convert the js value (except null and undefined) into a string; 2, use the "" + value "statement; 3, use the" String (value) "statement.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

The three ways javascript converts a variable to a string are as follows:

Value.toString ()

"" + value

String (value)

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

"" + value: add value to an empty string to convert it to a string. This approach is actually a slightly obscure technique that may make it difficult for others to understand the developer's intentions. However, there are different opinions on this point, and some people prefer this approach.

String (value): this approach is very clear: use the String () function to convert value to a string. However, there are two different uses of String (), which can be confused, especially for Java developers. When String () is used with the operator new as a constructor, it returns a newly created String object; when String () is called without the new operator, it simply converts value to the original string. The two are very different:

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

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

Nuances between "+ value" and "String (value)"

Both "+ value" and String (value) can convert value to strings. How do they do that? In fact, although the results are the same, the methods are slightly different.

Convert primitive base types to strings

Both methods use the internal function ToString () to convert the primitive primitive type to a 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 the primitive primitive type to a string:

Parameter result undefined "undefined" null "null" Boolean "true" or "false" Number converts a number to a string, for example: "1.765" String does not need conversion

Convert Object to string

Both methods convert Object to primitive before converting to a string. The difference is that "+ value uses the internal function ToPrimitive (Number) (except for the 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 (); return if the result is primitive; otherwise, return TypeError.

ToPrimitive (String): similar to ToPrimitive (Number), except that obj.toString () is called first, followed by obj.valueOf ().

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

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

Call result:

> "" + objvalueOftoStringTypeError: Cannot convert object to primitive value > String (obj) toStringvalueOfTypeError: Cannot convert object to primitive value

Their results are the same.

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

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

Because the return value of valueOf () is not primitive, ToPrimitive (Number) skips valueOf () and returns the return value of toString (). In this way, the return value is the same as that of ToPrimitive (String).

When object is an instance of Boolean, Number, or String, valueOf () returns primitive. This means that the calculation process of the two is as follows:

ToPrimitive (Number): valueOf () returns a prized value, which is then converted to a string using ToString ().

ToPrimitive (String): toString () converts the prized value to a string through the ToString () function.

It is known that although the calculation process is different, their results are the same.

Thank you for your reading, the above is the content of "how javascript converts variables into strings". After the study of this article, I believe you have a deeper understanding of how javascript converts variables into strings, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report