1

Consider the following javascript code.

function myFunction()
{
    var myArray = [];
    myArray.push("hello");
    return myArray;
}

var isValid = myFunction();

Is the return value referring to a stack variable myArray that is lost when myFunction returned? I am from C/C++ background - hence wondering how this actually works.

winterlight
  • 460
  • 4
  • 10
  • 1
    Data in JavaScript is garbage collected when all references to the object are overwritten or go out of scope. In this case, you are returning the reference to the array and assigning it to `isValid`, so the array won't be garbage collected until `isValid` goes out of scope or is overwritten like `isValid = null`. – sbking Dec 15 '13 at 08:24
  • 1
    See [Garbage Collection](http://en.wikipedia.org/wiki/Garbage_collection_(computer_science)). An object in JavaScript *cannot* be GC'ed (read: "destroyed") until it is no longer *strongly reachable*; at some point after the object is no longer strongly reachable (i.e. there exist no more strong references that can be reached from a GC root) then it is *eligible* for reclamation (read: "being destroyed"): it's the same as in Java or C# or Ruby, etc, with slight differences on what establishes a root/scope and whatnot. – user2864740 Dec 15 '13 at 08:34

3 Answers3

2

No it is not lost, because you are returning the array pointed to by myArray from the function and assigning it to a new variable, it lives on.

Welcome to a garbage collected language where you can do things like this that you could not do in C++. As long as a reference to an object exists somewhere, anywhere, that object will be preserved and is useful. When the data becomes unreachable by any code (e.g. no remaining code has a reference to it), then the garbage collector will free it.

One thing to keep in mind is that local variables in a function are NOT strictly stack variables like they are in C++. They can actually live longer than the function's execution.

Look at this example:

function delayColor(el, color) {
    var background = complement(color);

    setTimeout(function() {
        el.style.color = color;
        el.style.backgroundColor = background;
    }, 9000);
}

The function delayColor() runs immediately and runs to completion. But because the anonymous function passed to setTimeout() has references to the function arguments and local variables, those live on and are not destroyed until sometime later when the setTimeout() callback function is executed and completes. This is called a closure and shows how even a function scope is garbage collected when not in use any more and doesn't automatically get cleaned up just because it's containing function finished normal execution.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • "They can actually live longer than the function's execution" means only when variable is globally (outside the function) defined ? – Anirudha Gupta Dec 15 '13 at 08:26
  • @AnirudhaGupta - when anything inside or outside the function retains a reference to the data. That "anything" can be assigned to a variable outside the function (e.g. in some other scope) or it can be a function closure as I've illustrated in the code I added to my answer. In the OP's case, the data lives on because it was returned from the function and assigned to another variable. – jfriend00 Dec 15 '13 at 08:30
1

The reference is not lost...

Since arrays are objects and there is still a reference to the object (the assigment from the return), the object is not garbage collected. You may use the returned object as you would like.

Objects declared within a function are not pushed onto the stack, but malloc'ed. As long as a reference to the object lives on, the object will not be garbage collected. Primitive values, like numbers and boolean, are simply returned by value.

function myFunction() {
    var myArray = [];
    myArray.push("hello");
    return myArray;
}

var isValid = myFunction();
alert(isValid[0]);

See example: http://jsfiddle.net/PJ35y/

Andy Jones
  • 6,205
  • 4
  • 31
  • 47
0

In this case the variable myArray has been returned. The isValid variable has the result of calling the function. Consider returning a pointer to int in C, how does it return a malloc()[ed] pointer "on the stack"?

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249