Primitive Wrapper Types in JS

Posted on by By Nikhilesh, in Front End, Javascript | 0

In this post, I’m going to focus on basic concepts of JavaScript. So we are going to learn the concept of primitive wrapper types in JavaScript which is confusing to most.

So, there are three primitive wrapper types and they are: 1: String, 2: Number and 3: Boolean. And these primitive wrapper types are reference types that are automatically created behind the scenes whenever these primitive wrappers types are read. Let’s consider an example to explain our point.

var name = "Lorem";
var firstChar = name.charAt(0);
console.log(firstChar);     // "L"

 

In the first line of the above code, a primitive string value has been assigned to name. The second line treats name like an object and calls charAt(0) using dot notation.

Now let us examine behind the scenes of the above code:

// what the JavaScript engine does
var name = "Lorem";
vat temp = new String(name);
var firstChar = name.charAt(0);
temp = null;
console.log(firstChar);     // "L"

 

Since the second line in the above code uses a string (a primitive) like an object, the JavaScript engine creates an instance of Sting so that charAt(0) will work. The Sting object exists only for one statement before it’s destroyed (a process which is called autoboxing). At this point, since we have said that it is an object, we might consider to add a property to it. So let us give it a try:

var name = "Lorem";
name.last = "Ipsum";

console.log(name.last);     // undefined

 

The above code tries to add the property last to the string name. And looking at the code, it just looks fine but wait, What happened? Why didn’t it work? When we are working with regular objects, we are free to add properties at any time and they stay until you manually remove them. But with primitive wrapper types, properties seem to disappear because the object on which the property was assigned is being destroyed immediately afterward.

And this happens in JavaScript engine:

// what the JavaScript engine does
var name = "Lorem";
vat temp = new String(name);
name.last = "Ipsum";
temp = null;                // temporary object destroyed

var temp = new String(name);
console.log(temp.last);     // undefined
temp = null;

 

We can see what JavaScript engine has done. Instead of assigning a new property to a string, the code has actually creates a new property on a temporary object that is already destroyed. And when you try to access that property later, a different object is temporarily created and the new property doesn’t exist there. Although reference values are created automatically for primitive values, when instanceof checks for these types of values, the result is false.

var name = "Lorem";
var count = 5;
var found = false;

console.log(name instanceof String);      //false
console.log(name instanceof Number);      //false
console.log(name instanceof Boolean);      //false

 

The instanceof operator returns false because a temporary object is created only when a value is read. Because instanceof doesn’t actually read anything, no temporary objects are created, and this tells us that values aren’t instances of primitive wrapper types. It has to be noted that we cannot use String, Number and Boolean objects as you would primitive values. Even though the typeof evaluated to true.

logo

Best Open Source Business Intelligence Software Helical Insight is Here

logo

A Business Intelligence Framework

-By
Uma Maheswar
Helical IT Solution

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments