javascript by reference and memory

javascript pass by reference
javascript heap memory
javascript reference variable
javascript object reference
javascript copy variable instead of reference
javascript pass string by reference
javascript copy value not reference
javascript memory leak

guys i am trying to learn some javascript and i have a question that i think is important to ask. so what i have gotten so far is that when i do something like this:

var a=function(){} 

during the creation phase there will be created in memory a variable (a) with undefined value and during the execution the a will point to the memory slot where the function lies.

so what happens to the old spot a was pointing at(the one with undefined value)? also if i set b equal to a this means they will point to the same memory slot right?

what happened to the slot that b was previously pointing at?

finally does the function gets saved to the memory during the creation phase but cant be called because nothing points at it or its just get saved during the execution phase?

thanks i hope you can help me make my mind clear (i cant sleep with these questions on my head :D)

so what happens to the old spot a was pointing at(the one with undefined value)?

I don't know the exact details of any JavaScript engines, but I suspect that there isn't really any such slot. Instead, undefined is probably just a special value that gets put into a that indicates that it doesn't point anywhere (similar to NULL in C).

If there really is a memory slot for undefined, it's a single object that all undefined variables point to. Nothing happens to it, since there are still lots of other variables pointing to it.

also if i set b equal to a this means they will point to the same memory slot right?

Yes. When you assign variables in JavaScript, it just copies the reference.

what happened to the slot that b was previously pointing at?

If anything else is still pointing to it, nothing. If nothing else is pointing to it, the garbage collector will eventually reclaim its memory.

finally does the function gets saved to the memory during the creation phase but cant be called because nothing points at it or its just get saved during the execution phase?

It gets saved when creating it. It's possible to create a function without ever assigning it to a variable -- this is a common idiom called Immediately Invoked Function Expression:

(function() { console.log("Function is running"); })();

Memory Management, References. The main concept that garbage collection algorithms rely on is the concept of reference. Within the context of memory management,  Within the context of memory management, an object is said to reference another object if the former has access to the latter (either implicitly or explicitly). For instance, a JavaScript object has a reference to its prototype (implicit reference) and to its properties values (explicit reference).

I think I understand what you are trying to ask. When you have a variable in JavaScript, or any programming language that has its own garbage collector, and you are referencing a part in memory... for instance lets say var A = 50; var B = 75;

and you want to turn around and re-assign A = B;

The memory location will be released and A will now be 75.

Here is some documentation that can explain in further detail: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management

javascript by reference and memory, so what happens to the old spot a was pointing at(the one with undefined value)?​. I don't know the exact details of any JavaScript engines, but I  When we use arr to do something, such as pushing a value, the Javascript engine goes to the location of arr in memory and works with the information stored there. Assigning by Reference When a reference type value, an object, is copied to another variable using = , the address of that value is what’s actually copied over as if it were a primitive.

var a=function(){}

That doesn't create a pointer to undefined and then repoints it to the function. It directly points it to the function.

Even if you did

var a;
a = function(){};

a doesn't point at a random memory place that holds undefined. A is undefined. Undefined is a value. And then you assign it with a reference pointing to the function. Undefined doesn't need to be cleared, because it's substituted.

Also, base values are copied, not referenced, so:

A = 50; var B = 75; A = B;

Doesn't use 3 memory locations, only 2, holding first 50 and 75, and then 75 and 75.

If it were objects, then it would be references. What happens to objects when they got unreferenced is that they are removed by the Garbage collector, that is a routine that frees the memory of objects no longer referenced by anyone.

How JavaScript works: memory management + how to handle 4 , Memory references. The main concept garbage collection algorithms rely on is the one of reference. Within the context of memory management,  That happens because, when dealing with objects, the =operator works by reference. What is really happening can be described as follows: A variable myName is created and is given the value of an object which has a property called firstName. firstName has the value of "Carlos". JavaScript allocates a memory spot for myName and the object it contains.

Grasp “By Value” and “By Reference” in JavaScript, JavaScript allocates a memory spot for it. A variable firstName is created and is given a copy of name 's value. firstName has its own memory  Pass-by-reference means that you are passing in not the value of the variable but the actual memory location of the variable. You are passing the reference to the value. So in this next example of

Value vs. Reference, A simple look at computer memory explains what's happening. JavaScript has 3 data types that are copied by having their reference copied: Array , Function  In hybrid system, where reference counting and garbage collection are used, memory leaks will occur because the system fails to identify circular references. Example The following example shows a circular reference between javascript object and DOM object.The javascript object has a reference to DOM object(Div) and Dom object(Div), through "expando" property, has a reference to javascript object(obj).

All you need to know on by reference vs by value, Strictly speaking in Ruby and JavaScript everything is copied by value. When it comes to objects though, the values happen to be the memory  In JavaScript every object has an implicit reference to its prototype and explicit reference to its property values. Reference count algorithm is the most basic garbage collector algorithm, It reduces the definition of “an object is not needed anymore” to “an object has no other objects referencing it”.