Deleting a variable from the window object (Not possible to delete if var is used)


Most of the times all javascript applications deals with objects and their properties. I used empty a property which I longer needed. After that I came to know about delete operator which used to remove a property from an object.

While using delete operator I got doubt like,

is it possible to delete a variable?

So the answer to the question depends on how the global variable or property is defined.
  • If it is created using var, it cannot be deleted.
               var global = 32;           // global is a variable created using 'var'
               delete global;               // returns false
               console.log(global)      // global is still 32

  • If it is created without var, it can be deleted.
               global = 1;                    // global is a variable created without using 'var'
               delete global;               // return true
               console.log(global);     // error, global is not defined

Explanation:

1. Using `var`

In this case reference global is created in "VariableEnvironment" that is attached to the current scope.
This may be a function execution context in the case of using var inside a function (though it may be get a little more complicated when you consider let)
or 
In the case of "global" code the VariableEnvironment is attached to the global object (often window).

References in VariableEnvironment are not normally deletable - unless your code is executed in an eval context (which most browser-based development consoles use), then variables declared with var cannot be deleted.

2. Without Using `var`

When trying to assign a value to a variable without using the `var` keyword, Javascript tries to locate the named reference in "LexicalEnvironment".

LexicalEnvironments are nested and has a parent ("outer environment reference") and when Javascript fails to locate the reference in a LexicalEnvironment, it looks in the parent LexicalEnvironment.

The top level LexicalEnvironment is the "global environment", and that is bound to the global object in that its references are the global object's properties.
So if you try to access a name that was not declared using a var keyword in the current scope or any outer scopes, Javascript will eventually fetch a property of the window object to serve as that reference.
As we've learned before, properties on objects can be deleted.

Notes

  1. It is important to remember that var declarations are "hoisted" - i.e. they are always considered to have happened in the beginning of the scope that they are in - though not the value initialisation that may be done in a var statement - that is left where it is. So in the following code, a is a reference from the VariableEnvironment and not the window property and its value will be 10 at the end of the code:

    function test() { a = 5; var a = 10;
  2. The above discussion is when "strict mode" is not enabled. Lookup rules are a bit different when using "strict mode" and lexical references that would have resolved to window properties without "strict mode" will raise "undeclared variable" errors under "strict mode". I didn't really understand where this is specified, but its how browsers behave.
  3. The delete command has no effect on regular variables, only properties. After the delete command the property doesn't have the value null, it doesn't exist at all.
    If the property is an object reference, the delete command deletes the property then the delete operator deletes only a reference, never an object itself. If it did delete the object itself, other remaining references would be dangling, like a C++ delete. (And accessing one of them would cause a crash. To make them all turn null would mean having extra work when deleting or extra memory for each object.)
  4. Since Javascript is garbage collected, you don't need to delete objects themselves - they will be removed when there is no way to refer to them anymore. It can be useful to delete references to an object if you are finished with them, because this gives the garbage collector more information about what is able to be reclaimed. If references remain to a large object, this can cause it to be unreclaimed - even if the rest of your program doesn't actually use that object.

Comments

Popular posts from this blog

conditional subschemas (writing conditions inside a JSON Schema)

JSON Schema validation keywords with more explanation

nodejs/javascript client for Rabbitmq