What are Wrapper Objects?

Primitive Values like string,number and boolean with the exception of null and undefined have properties and methods even though they are not objects.

let name = "marko";

console.log(typeof name); // logs  "string"
console.log(name.toUpperCase()); // logs  "MARKO"

name is a primitive string value that has no properties and methods but in this example we are calling a toUpperCase() method which does not throw an error but returns MARKO.

The reason for this is that the primitive value is temporarily converted or coerce to an object so the name variable behaves like an object. Every primitive except null and undefined have Wrapper Objects. The Wrapper Objects are String,Number,Boolean,Symbol and BigInt. In this case, the name.toUpperCase() invocation, behind the scenes it looks like this.


console.log(new String(name).toUpperCase()); // logs  "MARKO"

The newly created object is immediately discarded after we finished accessing a property or calling a method.