I am not a javascript programmer, but I'm trying to hack an improvement to an existing running program.
In C, a common error is to define a variable within a function, then return it. Of course the variable vanishes off the stack when the function exits.
In javascript functions, I don't want to assign to a variable without a var, as that gives it global scope. So I say var foo = something;, then at the end of the function return foo.
This seems to work. Does it happen to work, or is it guaranteed to work? Is it the case that as javascript is garbage collected, the variable is now pointed at by the calling scope, which preserves it?
I've asked google, but either I'm not searching in the right place, the answer is too complicated for me to find, or the answer is so trivial it's not even mentioned.
It occurs to me that I could return an expression, avoiding naming it altogether. Is this more sensible?