This article mainly introduces you to three methods of converting variables into strings in Javascript. These three methods are: value.toString(), "" + value and String(value). The article passes The sample code is introduced in very detail. Friends who need it can follow the editor to learn together.
Preface
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. Not much to say below, let’s take a look at the detailed introduction.
3 methods
The 3 methods to convert a variable into a string are as follows:
value. toString()
##"" + 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
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
as a constructor, so use it only Just convert the string.
""+Subtle differences between value 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 stringBoth methods use the internal function
ToString()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:
Result | |
---|---|
"undefined" | ##null |
Boolean | |
Number | |
##String | |
将Object转换为字符串 转换为字符串之前,两种方法都会先将Object转换为primitive。不同的是,""+value使用内部函数
可以通过以下示例了解区别,obj如下: var obj = { valueOf: function() { console.log("valueOf"); return {}; }, toString: function() { console.log("toString"); return {}; } }; Copy after login 调用结果: > "" + obj valueOf toString TypeError: Cannot convert object to primitive value > String(obj) toString valueOf TypeError: Cannot convert object to primitive value Copy after login 它们的结果相同
> var x = {} > x.valueOf() === x true Copy after login 由于 当object是Boolean、Number或者String实例时,
可知,虽然计算过程不同,但是它们的结果是一样的。 结论 那么你该选择哪种方法呢?如果你可以确保value值不是null和undefined,那么不妨使用 总结 The above is the detailed content of Three ways to convert variables into strings in Javascript. For more information, please follow other related articles on the PHP Chinese website!
Related labels:
source:php.cn
Previous article:JavaScript is natively compatible with IE6 and can control the implementation of scrolling text.
Next article:js implements data structure: tree and binary tree, binary tree traversal and basic operation methods
Statement of this Website
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
Latest Articles by Author
Latest Issues
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
|