views:

7496

answers:

3

I have a global variable in Javascript (actually a window property, but I don't think it matters) which was already populated by a previous script but I don't want another script that will run later to see its value or that it was even defined.

I've put some_var = undefined and it works for the purpose of testing typeof some_var == "undefined" but I really do not think its the write way to go about it.

What do you think?

+12  A: 
some_var = null;

//or remove it..
delete some_var;
scunliffe
+1 for the proper way of calling delete.
Robert Koritnik
This doesn't work if the scope of this code is a function. See @noah's answer for the correct solution.
Roatin Marth
Thanks for the answer, but I've accepted Noah's answer because it better explains the pitfalls of `delete`.
Guss
no worries... I gave a "quick n dirty" simple answer - @noah added all the details for the "other" cases thus he deserves credit too. ;-)
scunliffe
+3  A: 

If you are implicitly declaring the variable without var, the proper way would be to use delete foo.

However after you delete it, if you try to use this in an operation such as addition a ReferenceError will be thrown because you can't add a string to an undeclared, undefined identifier. Example:

x = 5;
delete x
alert('foo' + x )
// ReferenceError: x is not defined

It may be safer in some situations to assign it to false, null, or undefined so it's declared and won't throw this type of error.

foo = false

Note that in ECMAScript null, false, undefined, 0, NaN, or '' would all evaluate to false. Just make sure you dont use the !== operator but instead != when type checking for booleans and you don't want identity checking ( so null would == false and false == undefined ).

Also note that delete doesn't "delete" references but just properties directly on the object, eg:

bah = {}, foo = {}; bah.ref = foo;

delete bah.ref;
alert( [bah.ref, foo ] )
// ,[object Object] ( it deleted the property but not the reference to the other object )

If you have declared a variable with var you can't delete it:

(function() {
    var x = 5;
    alert(delete x)
    // false
})();

In Rhino:

js> var x
js> delete x
false

Nor can you delete some predefined properties like Math.PI:

js> delete Math.PI
false

There are some odd exceptions to delete as with any language, if you care enough you should read:

meder
+1 for the complete explanation about the "delete behavior".
GmonC
Thanks for the complete answer with all the details. I marked it up for this, but I've accepted Noah's answer because I believe that for a simple question brevity is more important then completion. Again - thanks for the great work you did on this answer.
Guss
+10  A: 

@scunlife's answer will work, but technically it ought to be

delete window.some_var; 

delete is supposed to be a no-op when the target isn't an object property. e.g.,

(function() {
   var foo = 123;
   delete foo; // wont do anything, foo is still 123
   var bar = { foo: 123 };
   delete bar.foo; // foo is gone
}());

But since global variables are actually members of the window object, it works.

When prototype chains are involved, using delete gets more complex because it only removes the property from the target object, and not the prototype. e.g.,

function Foo() {}
Foo.prototype = { bar: 123 };
var foo = new Foo();
// foo.bar is 123
foo.bar = 456;
// foo.bar is now 456
delete foo.bar;
// foo.bar is 123 again.

So be careful.

EDIT: My answer is somewhat inaccurate (see "Misconceptions" at the end). The link explains all the gory details, but the summary is that there can be big differences between browsers and depending on the object you are deleting from. delete object.someProp should generally be safe as long as object !== window. I still wouldn't use it to delete variables declared with var although you can under the right circumstances.

noah
http://perfectionkills.com/understanding-delete/
jedierikb