I have seen javascript values set to null at the end of a function.
Is this done to reduce memory usage or just to prevent accidental u
It wouldn't make any difference setting a local variable to null at the end of the function because it would be removed from the stack when it returns anyway.
However, inside of a closure, the variable will not be deallocated.
var fn = function() {
var local = 0;
return function() {
console.log(++local);
}
}
var returned = fn();
returned(); // 1
returned(); // 2
jsFiddle.
When the inner function is returned, the outer variable's scope will live on, accessible to the returned inner function.