In this post I wrote about a program seemingly running as intended despite my not having declared a variable (with
var, anyway). (I'd have added on to that thread, but it's closed.)
You can also use delete to delete a global variable if the var keyword was not used to declare the variable:
g = 17;
I tested it with the following program:
g = 300; console.log(g); // 300 delete g; console.log(g); // ReferenceError: g is not defined
Would it then be correct to infer that
delete variableName can be used as an alternative to refreshing the browser to get rid of world-dominating variables?
Also, outside of this situation, when would
delete variable be a good idea? I've tried searching this myself, but the results I saw mostly seemed to be (outdated?) debates regarding whether this is possible, and or how, but not when, to do so.
To my nubly eyes, it seems like it would be preferable to remove an unnecessary variable from the program itself rather than to declare one, then delete it, and, again through my nubvision™ with my nubbily short programs, it seems like space taken up by a variable is theoretically not a big dill. Any feedback regarding either or both questions would be greatly appreciated.
Replace this line with your code.