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 usage elsewhere?
Is there a good case for doing this. If so when?
var myValue; . . . myValue = null;
As soon as you use the variable declare it as null or "" because active variables in js can be read using inspect element. Resetting the variable password to null will keep your password information safe.
The set <variable> to null construct sets a project variable to no value. <variable> refers to the variable of bound entity that is referenced in the agent descriptor file.
Only use null if you explicitly want to denote the value of a variable as having "no value". As @com2gz states: null is used to define something programmatically empty. undefined is meant to say that the reference is not existing. A null value has a defined reference to "nothing".
You can have an entire programming language without NULL. The problem with NULL is that it is a non-value value, a sentinel, a special case that was lumped in with everything else. Instead, we need an entity that contains information about (1) whether it contains a value and (2) the contained value, if it exists.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With