A reflection on love scarcity

I will never forget that call, the first of many calls that I wish were just bad dreams. Dad was in the ER, his organs were failing. The doctors were trying to figure out what was wrong. In the wake…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Reference and Value Types in Javascript

Hi everyone :)

In this very new of my article, I want to explain to you one of the most important cases In Javascript. This subject is asked in interviews most of the time and maybe most people don’t know about it or finds it difficult to understand. I will try to explain it simply as much as I can. I hope you enjoy.

So to begin with,

To be able to understand what is Reference or Value Type, we need to understand what is Heap and Stack in Javascript first.

Heap and Stack are actually memory types in Javascript. Javascript has to store assigned values in its memory. So that’s why it has heap and stack. But what are the differences ?

I will explain it deeper but very very shortly,

Heap: is the long time memory of Js.

Stack: is the short-term memory of Js.

Let’s see with a bit of code how this works:

We created a function that takes user’s name and displays it.

As we know, there are two data types in Js: Primitive and Non-primitive

As follows, there are two types in Js: Value Types and Reference Types

> Let’s start with Value Types:

Value Types consist of Strings, Numbers, Booleans, undefined, null and Symbol.

Let’s see this with an example:

As you see in this example, we copied our original variable to a new one and we changed the original one’s value. Original variable has changed but copied variable remained the same. Because it is a copied variable. A change in the original variable does not affect the copied variable.

Let’s see another example:

> Let’s go on with Reference Types:

Reference Types consist of Objects, Arrays, Functions

I know it sounds a bit complicated but let’s see with a small picture to understand it better.

And now let’s see it with a bit of code examples:

Now you might surprise here, because in value types, copied variable did not change. But here both arrays changed. And you will wonder why ?

It is because both variables hold the same address/pointer. We actually copied the original variable’s address here, not the value itself. It means we never copied the array itself. Thus, if you change one array, you automatically change the other.

Same for objects;

=> Spread Operator

=> Object.assign

Let’s move on with some other examples:

Now you will ask why ? Because they exactly have the same data, they exactly look like the same.

It is because they are actually different objects even if they hold same data because they are at two different addresses in memory. We said that Js creates a pointer for non-primitive datas. So both objects will be created at different addresses. person1 is a different object with its own address/pointer and person2 is a whole different object with its own address/pointer.

But

Let’s see another important example:

We think this will throw and error, right ? Because it is a const. Cannot be changed. But it will work. Because again, we store the address in the constant. Data is manipulated, yes but we don’t change array ( the address ) directly. So our array is still at the same address.

But if you try to do this, you will get an error;

Because you try to change the array ( the address ) directly. Pay attention to the equal sign. The equal sign here tries to assign/create a new array, a new address. And you cannot change your array like this with const.

Same for objects;

So this was all about Value and Reference Types. I hope you enjoyed it and I hope I can be helpful for those who find it difficult to understand.

You can follow me on Medium for more articles.

Cheers :)

Add a comment

Related posts:

Emotional affairs. The dream that turns into a nightmare

It was in the depths of a challenging period for our young family that I fell into the arms of the affair. With hindsight, I was a period in which I was feeling lost, exhausted, and anxious. I no…

Undeniable Proof That You Need Washington Dc Dispensaries

When you are a cannabis enthusiast in Washington, D.C., you might be most likely informed about dispensaries which provide a variety of items and experiences. Having said that, there are many…

The Best Way to Break a Fast in Ekadasi

Hello! In this article, you will discover the best way to break a fast on Ekadasi. Ekadasi is a sacred and significant day in the Hindu calendar, and is considered auspicious for the observance of a…