# How memory location is allocated for variable?

When ever we declare a variable , it allocate memory location for that variable right

for example

``````var a=5;
``````

Now I just declare a variable a and assign value as five. so It create a memory location
for a and assign value five right?

``````var d=6
var f=7
f=d
``````

The above code I wrote where i declare two variable, First variable is d and assign value 6 and second variable is f . So it create memory location for d and f separate and it assign the value 6 and 7 to d and f respectively
Now you see at third line where I mention f=d
that means the value of d will be assigned to f.

Now I will ask my doubt
So the first time when we declare two variable, two memory location was created.

the moment when we say

``````f=d
``````

will they share same memory location?

no, they are still two different variables that have the same value. You will notice so because if you change the value of one of the two like with `f++` the other doesnâ€™t change value

try instead with arrays - those works by reference:

``````let arr = [0, 0, 0, 0, 0];
let arr1 = arr;
arr1[3] = 1;
console.log(arr); // this is now [0, 0, 0, 1, 0]
``````
2 Likes

All that confusion is suddenly developed in my mind due to watching a video explaining about how to implement Linked List .
Thank you for replaying

Soâ€¦ it depends.
When you do

``````let a = 5;
let b = 6;
a = b;
``````

The value of `b` is assigned to `a` and theyâ€™re both two different `6`s. But if `b` is an object, what is actually stored in the variable is a memory address. Then when you assign `b` to `a`, both `b` and `a` contain the address to the same object. I know that this can get confusing.
In other languages, you actually declare a variable with a type and so itâ€™s a little more obvious what sort of value it contains.

So what what I understood then when we say
a=b you say that the memory address of b is available on a. So that a can access memory location of variable b. so a can access the value available on b! Am i right?

var person={name:â€śsomenameâ€ť}

See here above I create a object person and i added a property and name.
if we add property and value to the object? is there any new memory address created?

I meanâ€¦ every time you store something in memory it needs a memory address.

The distinction Iâ€™m talking about is between primitive types and reference types.