In the Native Engine
The garbage collector in the JavaScript engine periodically runs to look for memory on the heap that is no longer needed by the program. It works by traversing all object references on the heap to determine which objects are still accessible or "live", versus which are unreachable or "dead".
For objects that are no longer referenced by the program, the engine's garbage collector will free up the memory allocated to those objects on the heap so that the space can be reused for new object allocations.
No matter the specific strategy, the key purpose of garbage collection is the same - to find and free up spaces in memory that are no longer being used by the application. This process runs in the background of the JavaScript engine to manage memory without the programmer having to explicitly handle object lifecycles.