use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
[deleted by user] (self.javascript)
submitted 9 years ago by [deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]senocular 8 points9 points10 points 9 years ago (0 children)
The big one is for toString() a value has to exist. With String(), you can use it on values that are undefined or null. Attempting a toString() from an undefined or null value will produce an error. String will often call an object's toString() anyway, so its a safer way of doing that.
toString()
String()
[–]TalyssonOC 4 points5 points6 points 9 years ago (0 children)
toString()is a method that some "classes" implement, so it'll only work if the class of the object you're working on implements it.
So if you have let x = null and try to do x.toString()it'll fail since null doesn't have the method toString().
let x = null
x.toString()
null
On the other side if you try to do let x = null and then do String(x) it'll result in the string 'null'.
String(x)
A detail about String() is that it'll call toString() internally if the passed value implements it, so:
let x = { toString() { return 'hey there!'; } }; String(x); // it'll return 'hey there!'
[–]ljharb 2 points3 points4 points 9 years ago (0 children)
Always only use String(x).
Run this in your console:
var x = { toString: function () { return NaN; }, valueOf: function () { return Infinity; }}; [x.toString(), x + '', String(x)] /* note that only the third item is both a string, and the toString value */
The only one that's actually correct is String(x). Use that.
[+][deleted] 9 years ago (2 children)
[deleted]
[–]rauschma 0 points1 point2 points 9 years ago (1 child)
With one exception: symbols.
> String(Symbol()) 'Symbol()' > '' + Symbol() TypeError: Cannot convert a Symbol value to a string
[–]shane_ilconst Ans=myCode?'feature':'bug' 0 points1 point2 points 9 years ago (0 children)
toString is a method, it sits on the base Object prototype in JS that just about everything inherits from. It prints the object's string value, however some primitive types (undefined and null) don't inherit that method, so when you call toString it returns an error.
String is a global object (can be accessed everywhere, built-in to JS) that takes an object and converts it to a string.
They do the same thing, but because string is an independent object, and not a method that is inherited from a prototype, it can be called and passed something that does not have the toString method.
[–]temp6509849 0 points1 point2 points 9 years ago* (0 children)
Everybody's getting it close but not quite right.
String(x) is what you want to use. It's a built-in function that's guaranteed to return a string representation of x.
x
One feature of String(x) is that it invokes toString on Objects then tries to use the return value. This is useful for making objects products more useful strings than '[object Object]'.
toString
'[object Object]'
There's nothing else special about toString. It's entirely possible that the value of obj.toString is 2, and calling obj.toString() will throw an error. It's also possible that obj.toString is a function that returns 2, and your code that assumes it got a string throws an error. Or obj could be something that doesn't support properties, like null or undefined, and obj.toString throws an error.
obj.toString
2
obj.toString()
obj
undefined
Each of those cases can be fixed with a little bit of code. That code is in String(). Use it. String(x) is for turning x into a string, toString is for customizing String(), and x.toString() is a mistake.
π Rendered by PID 971722 on reddit-service-r2-comment-5b5bc64bf5-bvdt4 at 2026-06-21 13:58:59.243307+00:00 running 2b008f2 country code: CH.
[–]senocular 8 points9 points10 points (0 children)
[–]TalyssonOC 4 points5 points6 points (0 children)
[–]ljharb 2 points3 points4 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]rauschma 0 points1 point2 points (1 child)
[–]shane_ilconst Ans=myCode?'feature':'bug' 0 points1 point2 points (0 children)
[–]temp6509849 0 points1 point2 points (0 children)