What is a primitive wrapper object?

Am working on the full development path and after using the Number object I saw it described as a primitive wrapper object. Coming from C++ I understand there are primitive types like int and float and some types that are part of the standard library, like std::string, but I am confused on what primitive object means, let alone it being a wrapper. Any help?

Primitives are literals such as numbers, strings, booleans, and undefined. They have no type until JavaScript identifies them, then assigns the wrapper object that best fits. A number gets a Number object wrapper, a string (text in quotes) gets a String object wrapper, a boolean (true or false) gets a Bool object wrapper, and undefined stays as undefined (it is not an object and has no type).

Wrapper objects are what give primitives a data type, as well as a constructor prototype from which to inherit methods.

"string primitive".toUpperCase()    =>  STRING PRIMITIVE

There are two steps to the above. The primitive has no methods since it is not an object. JS must first cast it to type ‘string’, before it can inherit the .toUpperCase() method. If we attempt to call a method on a string that is not a String method, JS will throw a TypeError.

1 Like