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;
}
}
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;
}
}
Share
Improve this question
edited Sep 21, 2018 at 6:07
Adriaan
18.2k7 gold badges44 silver badges86 bronze badges
asked Aug 27, 2018 at 10:55
user9026111user9026111
5
- can u check stackoverflow./questions/1596782/… – Sathish Thangaraj Commented Aug 27, 2018 at 10:58
-
1
You don't want to remove object
variable
itself, you actually want to remove a property fromglobal
which name is stored invariable
. You need to calldelete global[variable]
to do this. – Yeldar Kurmangaliyev Commented Aug 27, 2018 at 10:58 - @YeldarKurmangaliyev Please post this as an answers… – feeela Commented Aug 27, 2018 at 11:00
- Thanks for you help Yeldar. Appreciate it! – user9026111 Commented Aug 27, 2018 at 11:03
- You can't delete a variable declared using var. See this – Shashank Poojary Commented Aug 27, 2018 at 11:04
3 Answers
Reset to default 4As 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 havevar 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.)
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
You can also use -
delete.[variableName]
after its work is done.