JS: Primitive Value
Primitive Value
A primitive value is a value whose Type is a primitive type.
Primitive Value Object Wrapper
many primitive types have a corresponding object wrapper class.
“Autoboxing” object wrapper on primitives
Some primitive values have object wrapper, so that you can call methods on them.
For example, when you want to know the length of string, you do
console.log("abc".length);
.length is a property access, and property is available only for object.
what happens is that JavaScript has objects for many primitive types, so that lets you to have methods on primitives, such as string and nunmbers.
when you call a method on a primitive, JavaScript temporarily converts the primitive to a corresponding object, calls the method, then convert it back to primitive value.
Convert Primitive Value to Object
You can create object version of the primitives yourself.
or use the constructors:
const aa = Object("abc"); const bb = new String("abc"); console.log(aa); // [String: "abc"] console.log(bb); // [String: "abc"] console.log(typeof aa === "object"); // true console.log(typeof bb === "object"); // true console.log(String(aa) === String(bb)); // true