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 it's the right way to go about it.
What do you think?
TLDR: simple defined variables (without
var
,let
,const
) could be deleted withdelete
. If you usevar
,let
,const
- they could not be deleted neither withdelete
nor withReflect.deleteProperty
.Chrome 55:
FF Nightly 53.0a1 shows same behaviour.
The delete operator removes a property from an object.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete
According to the question you need one of followings
@scunlife's answer will work, but technically it ought to be
delete is supposed to be a no-op when the target isn't an object property. e.g.,
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.,
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 asobject !== window
. I still wouldn't use it to delete variables declared withvar
although you can under the right circumstances.If you are implicitly declaring the variable without
var
, the proper way would be to usedelete 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: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.
Note that in ECMAScript
null
,false
,undefined
,0
,NaN
, or''
would all evaluate tofalse
. Just make sure you dont use the!==
operator but instead!=
when type checking for booleans and you don't want identity checking (sonull
would== false
andfalse == undefined
).Also note that
delete
doesn't "delete" references but just properties directly on the object, e.g.:If you have declared a variable with
var
you can't delete it:In Rhino:
Nor can you delete some predefined properties like
Math.PI
:There are some odd exceptions to
delete
as with any language, if you care enough you should read:You cannot delete a variable if you declared it (with var x;) at the time of first use. However, if your variable x first appeared in the script without a declaration, then you can use the delete operator (delete x;) and your variable will be deleted, very similar to deleting an element of an array or deleting a property of an object.