1

I'd like to delete all global variables that begin with 'Nwxp'.

Therefore I iterate over all global variables.

Check if the beginning is equal with my string and then delete them.

However, the delete keyword doesn't delete my variables. What am I doing wrong?

Here's my code:

var strNwxp    = "Nwxp";

for (var variable in global){
    var toCheck = variable.substring(0, 4);
    if (strNwxp === toCheck) {
        delete variable;
    }
}
Adriaan
  • 17,741
  • 7
  • 42
  • 75

3 Answers3

4

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:

  1. 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.

  2. 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.)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

Your code is trying to remove a property called variable.
Instead, as far as I understand, you actually want to remove a property from global which name is stored in variable as a string.

You need to call delete global[variable] to do this:

var strNwxp = "Nwxp";

for (var variable in global)
{
    var toCheck = variable.substring(0, 4);
    if (strNwxp === toCheck) {
        delete global[variable];
    }
}

It assumes that Nwxp* properties are assigned directly to global not with var.
Another answer describes it much better.

Demo snippet with global changed to window for browser environment:

window.NwxpHello = "World!";    
window.NwxpKey = "value";
window.NotNwxpKey = "another value";
    
var strNwxp = "Nwxp";

for (var variable in window)
{
    var toCheck = variable.substring(0, 4);
    if (strNwxp === toCheck) {
        delete window[variable];
    }
}

console.log(window.NwxpHello); // undefined, starts with "Nwxp" - was removed
console.log(window.NwxpKey); // undefined, starts with "Nwxp" - was removed
console.log(window.NotNwxpKey); // another value
Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
  • Are you sure this works in JavScript as it gives `Uncaught ReferenceError: global is not defined` – Ankit Agarwal Aug 27 '18 at 11:02
  • this doesn't work because variables declared with `var` are non-configurable (see `Object.getOwnPropertyDescriptor`. – Alnitak Aug 27 '18 at 11:02
  • 1
    @AnkitAgarwal - Presumably this is in an environment that defines `global` (such as Node.js). In browsers, that's roughly equivalent to `window`. – T.J. Crowder Aug 27 '18 at 11:03
  • @AnkitAgarwal I used `global`, because this is what the OP has used. I guess, he\she is working in Node.JS or other environment. – Yeldar Kurmangaliyev Aug 27 '18 at 11:03
  • @T.J.Crowder I agree with you but as a community user, we cannot assume that unless it is tagged with `NodeJS` or OP says so – Ankit Agarwal Aug 27 '18 at 11:04
  • 3
    @AnkitAgarwal - Yes, we can. The OP used `global` in their code, so an answer using `global` is perfectly acceptable. It may be Node.js, or it may be that the OP does `var global = this;` at global scope, or `var global = window;` in a browser, etc. That doesn't matter; they clearly have `global` defined somehow. – T.J. Crowder Aug 27 '18 at 11:05
  • also, if the variable truly is global you don't actually need to do `delete global[name]` you can just do `delete name`. – Alnitak Aug 27 '18 at 11:16
0

You can also use - delete.[variableName] after its work is done.