0

I know, that, for example, c++ has something like delete variable and it will be deleted from the memory. Is there something like this in JS. For example, I have var canSendRequest = true; and after some operations I want to delete it totally from the memory. As I read, delete canSendRequest will not work properly, but I did not manage to find a properly working way. Does someone know, how to do that?

ALEX
  • 55
  • 2
  • 8
  • JavaScript is garbage collected. If there are no references to the variable, it will go away by itself. – Barmar Aug 10 '20 at 18:59
  • As Barmar mentioned, There's garbage collector like in other programming languages, so any variable, when not used anymore, is collected. If you want to actively do so, you can give it undefined (or null) value ```myVar = undefined``` – Tzahi Leh Aug 10 '20 at 19:00
  • `delete canSendRequest` will work removing the variable from the current context. What do you mean with "will not work properly"? – F.Igor Aug 10 '20 at 19:04
  • Does this answer your question? [Memory release from local variable in JavaScript](https://stackoverflow.com/questions/2681511/memory-release-from-local-variable-in-javascript) – Heretic Monkey Aug 10 '20 at 19:07
  • So, if I make sth like this: ``` var v = 1; //some code with v //some code without v ``` The V variable will be automatically deleted after the part "hidden" behing the first // line – ALEX Aug 10 '20 at 19:07
  • 2
    @F.Igor: It won't. That's not what `delete` does in JS. It *sort of* works with globals under some circumstances, but that's all. –  Aug 10 '20 at 19:10
  • See also [How to unset a JavaScript variable?](https://stackoverflow.com/q/1596782/215552). The variable will not be "automatically deleted" so much as "scheduled to be removed if the runtime can determine it will not be used again". – Heretic Monkey Aug 10 '20 at 19:10
  • `Does this answer your question? Memory release from local variable in JavaScript ` I did not understand the only thing: if the variable is not inside any functions/collectors/prototypes and so on. It is just declared on the first line of the code, will it be totally forgooten after using delete – ALEX Aug 10 '20 at 19:10
  • `How to unset a JavaScript variable?.` I have seen that and that is why I ask this question: I want to delete the variable that is declared using var outside all functions – ALEX Aug 10 '20 at 19:11
  • You can't delete a global variable declared with `var`. Also, garbage collector doesn't collect any global variables nevertheless it is used later or not. The solution is not to use global variables at all, everything else is traced and unused variables are removed automatically by GC. – Teemu Aug 10 '20 at 19:45

2 Answers2

1

Since JS is garbage collected, you don't have direct control over the release of memory.

You just have to wait for the runtime to free the memory, which it will if there are no permanent references to it via the global object or a permanently existing closure. In those cases, you need to eliminate such references to the variable so that it qualifies to be freed.

If you're saying you want to eliminate the identifier itself from the scope, you definitely can't unless it was a global that was set as a property of the global object, not using proper variable declaration syntax.

0

Var is Hoisting type use let or const. like var g_a = 1; //create with var, g_a is a variable delete g_a; //return false console.log(g_a); //g_a is still 1

now try with let and const

Nitish Kumar
  • 13
  • 1
  • 5
  • `let` and `const` are "hoisted" as well, and `delete` operator is not purposed to delete variables, "_g\_a is still 1_" nevertheless what declaration type you'd use here. – Teemu Aug 11 '20 at 06:23