How to delete a variable in Javascript?

Asked

Viewed 8,317 times

21

I program in other languages, I’m still starting in Javascript. I was testing the mgibsonbr response code to this question and found a difficulty that may be trivial.

If I define var x = 0 in the global environment how then do I delete it? Elsewhere, I saw as "answer" set var x = null, but this does not remove the variable, it just assigns the null value to it.

I also tested the delete 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 10 and 10 appear, IE, the variable was not deleted.

Questions:

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

    Cannot delete variables declared with var in front.

2 answers

20


No way to delete variables declared with var

The function of the operator delete is to exclude properties of objects, not variables. Therefore 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 the object window? Yes. And yet I can’t rule her out? Not. Why?

Environment Records

In Javascript, variables are considered properties of internal objects (Environment Records) that represent a certain scope and are not exposed to 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 defining certain aspects of their behaviour. One of them, called [[Configurable]] in the specification, defines whether the property can be excluded or not (among other restrictions).

In browsers, created global variables are always properties of window. Those that are created with declaration (var) receive value false for the attribute [[Configurable]], and this prevents them from being deleted with delete window.varname. Already implicit global, created without var, follow a different path by the internal operations of the language, the end up receiving value true to [[Configurable]], allowing them to be excluded with delete window.varname. This can be considered a loophole in the language. It is good to note that it is advisable to avoid as far as possible the implicit global, who are also prohibited in the strict manner of language (the attempt to create them casts an exception).

Nonglobal variables

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

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

To exclude 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 release the corresponding memory if there is no reference left to the value that the variable contained (in the case of type values Object or derivatives, there may be multiple variables pointing to the same object or parts of it).

11

According to the MDN, the delete serves to remove properties of 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 statement x = 42, the x does not seem to be the property of an object, which can cause some confusion. In fact we are assigning a value to a property x, that a global object belongs to.

var's and function names are also properties, but with different internal attributes, as explained in @bfavaretto’s reply. In such cases the delete doesn’t work.

In short: when 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 the use of 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 have already commented, you can define the variable in question as null or undefined.

References:

  • 1

    complement: http://stackoverflow.com/questions/16963066/javascript-how-to-deleta-variable

  • Michael, cannot remove a variable defined with var under no circumstances? Why?

  • 1

    Because delete is not applicable to variable names and function names. It is only applicable to object properties. I supplemented the answer.

  • 1

    Every variable or global function (including what is declared with var) is the property of the global object. But they cannot be deleted due to an internal attribute (see my reply, and the first reference you mentioned, which explains this in great detail).

  • 2

    I understand. I will correct my statement in the reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.