As Yeldar Kurmangaliyev points out, you're deleting the wrong thing. But even if you delete the right thing, global variables created with var
cannot be deleted. delete
results in false
in that case (true
if the variable was created in a way it can be deleted, such as global.foo = 42
). So if the globals you're trying to get rid of were created that way, it won't work.
Separately, note that not all global variables are properties of the global object anymore, not as of ES2015. Specifically, let
, const
, and class
at global scope create globals that are not properties of the global object. (If you're using Node.js, you probably aren't using those at global scope, however.)
To ensure you can remove the global variable, don't use var
, use assignment to a property on global
:
global.Nwxplaskdflk = 42;
That global can be deleted.
A couple of notes:
I'm assuming global
is a reference to the global object in your code. E.g., you're using Node.js or a similar environment that creates it, or you have var global = this;
at global scope in your own code.
In general, if you can avoid creating globals, avoid creating globals. :-) Instead, use properties on some other object. (If the variable referring to that object has to be global for some reason, so be it, but the global namespace is very crowded, it's best to avoid adding to it.)