re: Why you should stop declaring variables inside a for loop (especially in JavaScript) VIEW POST


In Javascript, there isn't a Garbage collector system, so you need to handle yourself the resources management part of your application, and this could be a big mess.

Why? JavaScript has automatic garbage collection mechanism.


Yes sorry, I mean than "you cannot manually deallocate resources" like c# or c++ malloc or free


Sure you can, if you null an Object then it will be marked for GC. It may not happen immediately, but when the GC runs it will be cleaned up.

An object is said to be "garbage", or collectible if there are zero references pointing to it.



Unfortunately we just have to trust that is what is happening but it's okay, we don't want to panic anyone. Can you see what I did there.


C# has garbage collection and no way to manually free resources. It works the same as javascript, you can set all references to an object to null and then it will eventually be garbage collected. You can also force the garbage collector to run, but that is highly discouraged in almost all circumstances.

code of conduct - report abuse