Take the 2-minute tour ×

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;
share|improve this question
Is there a case for setting to null or using delete for performance or optimizing memory usage –  bob Aug 25 '11 at 12:04
add comment

4 Answers

up vote 10 down vote accepted

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() {


var returned = fn();

returned(); // 1
returned(); // 2


When the inner function is returned, the outer variable's scope will live on, accessible to the returned inner function.

share|improve this answer
unless the variable was not declared using "var" –  bob Aug 25 '11 at 12:00
@bob ...and the variable is not local to the function. –  fireshadow52 Aug 25 '11 at 12:02
There may be some specific issues relating to variables referencing DOM elements in IE. Take this code for example from jQuery source where a number of such variables are set to null. –  user113716 Aug 25 '11 at 12:16
Have you considered closures? –  RobG Aug 25 '11 at 12:17
@RobG Good call, I had not. I'll make an edit. –  alex Aug 25 '11 at 12:50
show 1 more comment

There was an issue with iE and circular references involving DOM elements. This was usually created when attaching listeners as follows:

function doStuff() {
   var el = document.getElementById('someID');
   el.onclick = function() {
     // has closure to el

   // remove reference to element
   el = null;

When doStuff ia called, the anonymous function attached to the element has a closure back to the element - a circular reference. In older versions of IE this caused a memory leak so the solution was to set el to null (or anything other than el) to break the circular reference after the assignment to el.onclick.

The second reason to use it is similar - if a function has a closure that keeps a reference a large object that might otherwise be garbage collected, then it may be useful to remove the reference so that the object can be cleaned up:

var someFunction = (function() {
  var x = reallyBigObject;
  // do stuff that uses reallyBigObject

  // Remove reference to reallyBigObject if it's no longer required
  x = null;

  return function() {
    // closure to x is unavoidable, but reference to reallyBigObject isn't required

So if the function returned to someFunction doesn't actually need to reference reallyBigObject, then the reference can be removed so the closure doesn't keep it from being garbage collected unnecessarily.

Note that you can't prevent the closure to variables in scope, but you can modify their values.

This stuff usually isn't much of an issue unless you are keeping pages open for a long time and doing lots of AJAX and adding and removing lots of DOM nodes. But it's probably a good habit to remove references to objects in closures where they aren't needed.

share|improve this answer
+1 for in depth answer. –  alex Aug 25 '11 at 12:54
In your second example, do you need the second var with x? –  alex Aug 25 '11 at 13:06
No, cut 'n paste typo. –  RobG Aug 25 '11 at 13:32
How will you execute the 'x = null;' line if you return something before that? you will never reach that line of code. –  Antonis Feb 5 at 12:19
Ah yes, my oversight. –  RobG Feb 5 at 22:49
add comment

No it isn't. Every local variable is deleted after the end of a function.

share|improve this answer
I wonder why someone downvoted my answer which was correct. Please leave a comment where I was mistaken. –  avall Sep 16 '13 at 7:31
add comment

Objects are destroyed (and their memories free'd) with the delete operator.

var myValue = new Object;
delete myValue;
// myValue will now be undefined

Works also for non new-alloced variables: [edit] Well, mistaken.

Read this document for more informations about delete : http://perfectionkills.com/understanding-delete/

share|improve this answer
delete will delete properties, not variables: jsfiddle.net/interdream/r58qk –  James Allardice Aug 25 '11 at 11:54
That's strange, I've tried example from my post in my chrome console, and it worked as specified (myValue was undefined). Thanks for enlightening. –  Maël Nison Aug 25 '11 at 11:59
The console is different, so don't always rely on it... see this question: stackoverflow.com/questions/7111612/how-does-console-log-work –  James Allardice Aug 25 '11 at 12:01
add comment

Your Answer


