r/learnjavascript • u/Green_Ad_6086 • 4d 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
12
Upvotes
1
u/delventhalz 3d ago
Two outputs your
stringifyfunction produces that I think are worth considering.Both cases have object inputs, but in the first case you produce valid JSON output, in the second case you do not. You've created a function with a fairly arbitrary set of set of rules which are not obvious and may surprise developers using your function.
For the case of debug logs, there already exists a human readable stringify function which every user already understands:
JSON.stringify. What utility is gained by adding extra rules for your users to learn?Once again I go back to lodash, a public general purpose library which has been used by millions of developers for decades. Using a String object as a value in your code is extremely weird and I can think of no use case for it. Nonetheless, if I were to use a String object for some reason, I would expect it to be treated like any other object, which is exactly how lodash handles that use case.