How does JavaScript handle memory management?

JavaScript uses a garbage collection process to manage memory by automatically freeing memory used by objects that are no longer referenced. In most engines, including V8 (Chrome and Node.js), this is done using a “mark-and-sweep” algorithm. When objects are created, they occupy memory, and JavaScript marks objects as unreachable when there are no references to them. These are then collected to free up memory.