What is JavaScript garbage collection?

JavaScript automatically allocates memory, while a variable is declared. Garbage collection finds memory no longer used by the application and releases it since it is of no use. Garbage collector uses algorithms like Mark-and-sweep algorithm, to find the memory no longer used.

This algorithm is used to free memory when an object is unreachable. The garbage collector identifies the objects, which are reachable or unreachable. These unreachable objects get the treatment from the automatic garbage collector.

The Reference-Counting Garbage Collection is also used for garbage collection in JavaScript. The object will get automatically garbage collected if there are no references to it.