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;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It wouldn’t make any difference setting a local variable to
nullat 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.
jsFiddle.
When the inner function is returned, the outer variable’s scope will live on, accessible to the returned inner function.