How to delete a variable in JavaScript?

20

I'm programming in other languages, so I'm still starting with JavaScript. I was testing mgibsonbr response code to this question and found a difficulty that may be trivial.

If I set var x = 0 to the global environment then how do I delete it? In other places, I have seen how "answer" set var x = null , but this does not remove the variable, it just assigns the null value to it.

I've also tested delete o with the following code inside an HTML:

<script>
var x = 10
console.log(x)
delete window.x
console.log(x)
</script>

And when I open the Chrome console I get 10 and 10, so the variable has not been deleted.

Questions:

  • How to delete a variable defined with var ? If it is not possible, preferably explain why, the logic of it.
  • If there are, what are the differences between deleting a variable in a local environment (from a function, an object, for example), and a global environment?
  • asked by anonymous 28.04.2014 / 17:14

    2 answers

    19

    There is no way to delete variables declared with var

    The function of the delete operator is to exclude properties of objects, not variables. So in principle it would not serve to exclude variables.

    But if I declare var x = 10 and I can access it as window.x , then x is not a property of object window ? Yes. And still not Can I delete it? No Why?

    Environment records

    In JavaScript, variables are considered environment object objects that represent a particular scope and are not exposed to the language - the fact that the global object is exposed as window in browsers is a special case . Object properties, in turn, also have internal attributes that define certain aspects of their behavior. One of them, called [[Configurable]] in the specification, defines whether or not the property can be deleted (among other constraints).)

    In browsers, global variables created are always window properties. Those that are created with declaration ( var ) are given false value for the [[Configurable]] attribute, and this prevents them from being deleted with delete window.varname . Implicit global, created without var , follow a different path by the internal operations of the language, they end up receiving true value for [[Configurable]], allowing them to be excluded with delete window.varname . This can be considered a breach in language. It's good to note that we recommend avoiding implicit globals . , which are also prohibited by strict mode of the language (the attempt to create them throws an exception).

    Non-global variables

    There is no way to exclude non-global variables, for two reasons:

    • The internal object that contains them is not exposed by the language
    • Even if it were exposed, variables are represented as properties with [[Configurable]]: false, and can not be excluded with delete .

    Why delete variables?

    A good reason to delete variables would be to free up memory. If this is your goal, simply set the value of the variable to null , and the garbage collector will free the corresponding memory if there is no reference left over to the value that the variable contained (in the case of values of type Object or derivatives, there can be multiple variables pointing to the same object or parts of it).

        
    28.04.2014 / 19:18
    11

    According to MDN, delete is used to remove properties from an object :

    x = 42;     // cria a propriedade x no objeto global
    var y = 43; // declara a variável y
    
    delete x; // retorna true  (x é uma propriedade global de um objeto e pode ser deletada)
    delete y; // retorna false (delete não afeta nomes de variável)
    

    In the x = 42 statement, x does not appear to be the property of an object, which can cause some confusion. In fact we are assigning a value to a x property, which belongs to a global object.

    var 'If function names are also properties, but with different internal attributes, as explained in @bfavaretto's response . In these cases delete does not work.

    To summarize: using var in the declaration, the variable can never be deleted.

    Example: without using var , it is possible (at least in Chrome) to delete the variable with or without window :

    // Não apaga
    var w = 789;
    delete w;
    
    // Não apaga
    var x = 123;
    delete window.x;
    
    // Apaga
    y = 123;
    delete window.y;
    
    // Apaga
    z = 456;
    delete z;
    

    Doing the same test, but within the scope of a function, the result is the same. If the goal is to get the error Uncaught ReferenceError: <alguma-coisa> is not defined , there is no other way. Otherwise, as you already mentioned, you can set the variable in question to null or undefined .

    References:

    28.04.2014 / 17:43