How do you convert something to a string in javascript?
ExamplesConvert different values to strings: Show
String(new Date()); Try it Yourself » Definition and UsageThe NoteThe String() method returns the same as the toString() method for any value. SyntaxParameters
Return Value
Browser Support
ES1 (JavaScript 1997) is fully supported in all browsers:
More ExamplesExamplesConvert different values to strings: String(Boolean(0)); Try it Yourself » If you’re following the Airbnb’s Style Guide, the preferred way is using “String()” 👍 It’s also the one I use because it’s the most explicit — making it easy for other people to follow the intention of your code 🤓 Remember the best code is not necessarily the most clever way, it’s the one that best communicates the understanding of your code to others… Warning: This blog post is outdated. Instead, read section “Converting to string” in “JavaScript for impatient programmers”. In JavaScript, there are three main ways in which any value can be converted to a string. This blog post explains each way, along with its advantages and disadvantages. Three approaches for converting to stringThe three approaches for converting to string are:
The problem with approach #1 is that it doesn’t work if the value is nullor undefined. That leaves us with approaches #2 and #3, which are basically equivalent.
A minor difference between ""+value and String(value)Until now you have heard that and String()convert their “argument” to string. But how do they actually do that? It turns out that they do it in slightly different ways, but usually arrive at the same result. Converting a primitives to stringBoth approaches use the internal ToString() operation to convert primitives to string. “Internal” means: a function specified by the ECMAScript 5.1 (§9.8) that isn’t accessible to the language itself. The following table explains how ToString() operates on primitives.
Converting objects to stringBoth approaches first convert an object to a primitive, before converting that primitive to string. However, +uses the internal ToPrimitive(Number) operation (except for dates [2]), while String()uses ToPrimitive(String).
With the following object, you can observe the difference: var obj = { valueOf: function () { console.log("valueOf"); return {}; // not a primitive, keep going }, toString: function () { console.log("toString"); return {}; // not a primitive, keep going } }; Interaction: > "" + obj valueOf toString TypeError: Cannot convert object to primitive value > String(obj) toString valueOf TypeError: Cannot convert object to primitive value The results are usually the sameThe above described difference rarely becomes apparent in practice. Here is why: Most objects use the default implementation of valueOf()which returns thisfor objects. > var x = {} > x.valueOf() === x true Hence, ToPrimitive(Number) will skip that method and return the result of toString(), just like ToPrimitive(String). If, however, an object is an instance of Boolean, or String, then valueOf()will return a primitive (the one wrapped by the object). That means that the two operations now compute their results as follows:
Thus, they both still arrive at the same destination, but by a different route. ConclusionWhich of the three approaches for converting to string should you choose? value.toString()can be OK, if you are sure that valuewill never be nullor undefined. Otherwise, ""+valueand String(value)are mostly equivalent. Which one people prefer is a matter of taste. I find String(value)more explicit. Related posts
How do you convert something into a string?Convert Object to String in java using toString() method of Object class or String. valueOf(object) method. Since there are mainly two types of class in java, i.e. user-defined class and predefined class such as StringBuilder or StringBuffer of whose objects can be converted into the string.
Is there a toString method in JavaScript?The toString() method is used internally by JavaScript when an object needs to be displayed as a text (like in HTML), or when an object needs to be used as a string.
|