I'm very confused about what the wrapper objects for primitives. For example, a string primitive and a string created with the string wrapper object.
var a = "aaaa";
var b = new String("bbbb");
console.log(a.toUpperCase()); // AAAA
console.log(b.toUpperCase()); // BBBB
console.log(typeof a); // string
console.log(typeof b); // object
Both give access to String.prototype
methods, and seem to act just like a string literal. But one is not a string, it's an object. What is the practical difference between a
and b
? Why would I create a string using new String()
?
A primitive string is not an object. An object string is an object.
Basically, that means:
Object strings are compared by reference, not by the string they contain
Object strings can store properties.