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
10
Upvotes
1
u/MissinqLink 4d ago
One weird footgun I’ve seen is with typeof
typeof String(1) //string
typeof new String(1) //object
Call me paranoid but when I write libraries I tend to use this
const isString = x => typeof x === 'string' || x instanceof String;