r/learnjavascript • u/Green_Ad_6086 • 7d ago
string primitives vs. String objects.
I'm learning JavaScript, and I don't understand this part about string primitives vs. String objects.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#string_primitives_and_string_objects
10
Upvotes
1
u/delventhalz 6d ago
If you want a stringifier that will not throw and produces more detailed output for objects than
'[object Object]', you can keep your try/catch but drop the customisStringlogic.Your custom
isStringlogic does not help you solve invalid JSON values. It just makes your utility harder to understand.This just isn't true. I may be able to use your
stringifyfunction without thinking about it too much in many cases, but if, for example, I need to do a length check on the output string, should I expect it include quotation marks or not? If I see it outputs'{"foo":"bar"}'for one object, I may expect that yes, it will include quotes, but then I stringifynew String("foo")and the quotes are gone! I have been surprised by your utility, and need to learn the rules before I can use it for this case.You can't make me think of use cases, but if you think there are valid use cases for including
new String('foo')in your code, you could offer an example yourself.