# Unboxing JavaScript

<span class="s"></span>

# Unboxing JavaScript

We all have that model in our mind that a variable is a box containing some value e.g. let a contains 10.

Let me ask you a simple question. What is a variable?

You might have answered that variable is a box in memory that holds some value and you have pictured something as below in your mind.

![A box named a containing value 10 and similar for b.](https://miro.medium.com/max/60/1*h13kXw7bVq5OABhmmW6slQ.jpeg?q=20)

<noscript><img alt="A box named a containing value 10 and similar for b." class="fg ev er ho w" src="https://miro.medium.com/max/2560/1*h13kXw7bVq5OABhmmW6slQ.jpeg" width="1280" height="720" srcSet="https://miro.medium.com/max/552/1*h13kXw7bVq5OABhmmW6slQ.jpeg 276w, https://miro.medium.com/max/1104/1*h13kXw7bVq5OABhmmW6slQ.jpeg 552w, https://miro.medium.com/max/1280/1*h13kXw7bVq5OABhmmW6slQ.jpeg 640w, https://miro.medium.com/max/1400/1*h13kXw7bVq5OABhmmW6slQ.jpeg 700w" sizes="700px"/></noscript>

Your general idea about variables.

But is that enough? Should we leave it just here?

No, to better understand the concepts of variables, objects, reference, etc in javascript we should dig a little deep to find out how this actually works.

Look a<span id="rmm"><span id="rmm">t</span></span> the illustration below and try to understand what is the difference in it with respect to one above.

![Image for post](https://miro.medium.com/max/60/1*C-Y-3zHV6YD5uyIUbeGdkg.jpeg?q=20)

<noscript><img alt="Image for post" class="fg ev er ho w" src="https://miro.medium.com/max/2560/1*C-Y-3zHV6YD5uyIUbeGdkg.jpeg" width="1280" height="720" srcSet="https://miro.medium.com/max/552/1*C-Y-3zHV6YD5uyIUbeGdkg.jpeg 276w, https://miro.medium.com/max/1104/1*C-Y-3zHV6YD5uyIUbeGdkg.jpeg 552w, https://miro.medium.com/max/1280/1*C-Y-3zHV6YD5uyIUbeGdkg.jpeg 640w, https://miro.medium.com/max/1400/1*C-Y-3zHV6YD5uyIUbeGdkg.jpeg 700w" sizes="700px"/></noscript>

We just unboxed the variable and the value.

A variable is just a reference to a value in memory. Imagine it like a thread from one point to another. So `a` is not `10` but it just points to `10` in the memory.

Consider the statements below :

`let a = 10;`

`a = 20;`

So does the value of `a` change? No, it is still the same thing just pointing to a new value. The illustration will help you to understand this better.

![Autoboxing in javascript](https://miro.medium.com/max/60/1*ZNyc3DnOfxC_rJs9EomSZQ.jpeg?q=20)

<noscript><img alt="Autoboxing in javascript" class="fg ev er ho w" src="https://miro.medium.com/max/2560/1*ZNyc3DnOfxC_rJs9EomSZQ.jpeg" width="1280" height="720" srcSet="https://miro.medium.com/max/552/1*ZNyc3DnOfxC_rJs9EomSZQ.jpeg 276w, https://miro.medium.com/max/1104/1*ZNyc3DnOfxC_rJs9EomSZQ.jpeg 552w, https://miro.medium.com/max/1280/1*ZNyc3DnOfxC_rJs9EomSZQ.jpeg 640w, https://miro.medium.com/max/1400/1*ZNyc3DnOfxC_rJs9EomSZQ.jpeg 700w" sizes="700px"/></noscript>

This process is called autoboxing.

Now let’s move on the almighty objects in JavaScript.

Read the following example code:

![Objects code example 1](https://miro.medium.com/max/60/1*9lkzdMuDl6qJZ17-sblBAA.png?q=20)

<noscript><img alt="Objects code example 1" class="fg ev er ho w" src="https://miro.medium.com/max/6464/1*9lkzdMuDl6qJZ17-sblBAA.png" width="3232" height="2364" srcSet="https://miro.medium.com/max/552/1*9lkzdMuDl6qJZ17-sblBAA.png 276w, https://miro.medium.com/max/1104/1*9lkzdMuDl6qJZ17-sblBAA.png 552w, https://miro.medium.com/max/1280/1*9lkzdMuDl6qJZ17-sblBAA.png 640w, https://miro.medium.com/max/1400/1*9lkzdMuDl6qJZ17-sblBAA.png 700w" sizes="700px"/></noscript>

Now try to imagine how this object will be stored in the memory. Hint: Try unboxing the pizza.

Done? So let’s see how this is actually represented.

![Image for post](https://miro.medium.com/max/60/1*_qJA8CRh3f0g7kCUMtMSAw.png?q=20)

<noscript><img alt="Image for post" class="fg ev er ho w" src="https://miro.medium.com/max/3840/1*_qJA8CRh3f0g7kCUMtMSAw.png" width="1920" height="1080" srcSet="https://miro.medium.com/max/552/1*_qJA8CRh3f0g7kCUMtMSAw.png 276w, https://miro.medium.com/max/1104/1*_qJA8CRh3f0g7kCUMtMSAw.png 552w, https://miro.medium.com/max/1280/1*_qJA8CRh3f0g7kCUMtMSAw.png 640w, https://miro.medium.com/max/1400/1*_qJA8CRh3f0g7kCUMtMSAw.png 700w" sizes="700px"/></noscript>

Your model might look almost the same except that the ingredients object is nested inside the pizza object but this is not the case.

Objects cannot be nested in the memory so the ingredient object itself is a separate entity and is referenced by the pizza object.

But the question is why should you even care about this?

Try reading the code below and find the output of it.

![Image for post](https://miro.medium.com/max/60/1*LhoddqiApSO_tSxvqglVyg.png?q=20)

<noscript><img alt="Image for post" class="fg ev er ho w" src="https://miro.medium.com/max/5784/1*LhoddqiApSO_tSxvqglVyg.png" width="2892" height="2732" srcSet="https://miro.medium.com/max/552/1*LhoddqiApSO_tSxvqglVyg.png 276w, https://miro.medium.com/max/1104/1*LhoddqiApSO_tSxvqglVyg.png 552w, https://miro.medium.com/max/1280/1*LhoddqiApSO_tSxvqglVyg.png 640w, https://miro.medium.com/max/1400/1*LhoddqiApSO_tSxvqglVyg.png 700w" sizes="700px"/></noscript>

What was your output?

Weird? Both of your pizzas are having 300g of cheese. To understand this just look at this illustration.

![Image for post](https://miro.medium.com/max/60/1*mI-2SayKSAtrQ9naaB8qbA.png?q=20)

<noscript><img alt="Image for post" class="fg ev er ho w" src="https://miro.medium.com/max/3840/1*mI-2SayKSAtrQ9naaB8qbA.png" width="1920" height="1080" srcSet="https://miro.medium.com/max/552/1*mI-2SayKSAtrQ9naaB8qbA.png 276w, https://miro.medium.com/max/1104/1*mI-2SayKSAtrQ9naaB8qbA.png 552w, https://miro.medium.com/max/1280/1*mI-2SayKSAtrQ9naaB8qbA.png 640w, https://miro.medium.com/max/1400/1*mI-2SayKSAtrQ9naaB8qbA.png 700w" sizes="700px"/></noscript>

Remember I told you that ingredients is a separate entity and is referenced by the `pizza` object. So when we created the `newPizza` we referenced the same entity and on changing the value in `newPizza` changed the entity itself. But both pizzas still referenced the same ingredients object.

So how to fix this? Actually, it’s quite simple.

![Image for post](https://miro.medium.com/max/60/1*HXyjf6DqDD0vNKUEpu39cw.png?q=20)

<noscript><img alt="Image for post" class="fg ev er ho w" src="https://miro.medium.com/max/4640/1*HXyjf6DqDD0vNKUEpu39cw.png" width="2320" height="1188" srcSet="https://miro.medium.com/max/552/1*HXyjf6DqDD0vNKUEpu39cw.png 276w, https://miro.medium.com/max/1104/1*HXyjf6DqDD0vNKUEpu39cw.png 552w, https://miro.medium.com/max/1280/1*HXyjf6DqDD0vNKUEpu39cw.png 640w, https://miro.medium.com/max/1400/1*HXyjf6DqDD0vNKUEpu39cw.png 700w" sizes="700px"/></noscript>

Fixing the issue.

So rather than referencing the previous ingredients object, we created a new one that in itself is a separate entity. Have a look at the illustration below.

![Image for post](https://miro.medium.com/max/60/1*VbohjFsNjJtdCrlwgdnRrg.png?q=20)

<noscript><img alt="Image for post" class="fg ev er ho w" src="https://miro.medium.com/max/3840/1*VbohjFsNjJtdCrlwgdnRrg.png" width="1920" height="1080" srcSet="https://miro.medium.com/max/552/1*VbohjFsNjJtdCrlwgdnRrg.png 276w, https://miro.medium.com/max/1104/1*VbohjFsNjJtdCrlwgdnRrg.png 552w, https://miro.medium.com/max/1280/1*VbohjFsNjJtdCrlwgdnRrg.png 640w, https://miro.medium.com/max/1400/1*VbohjFsNjJtdCrlwgdnRrg.png 700w" sizes="700px"/></noscript>

Finally, we have unboxed the javascript.

“ I hope that article helped you to understand how basic thing generally works under the hood and in future, you could easily unbox the JavaScript easily.
