All the languages I’ve had major exposure to (JS, Ruby, Java, Scala) implement their own garbage collection.

I was reading a bit about C last night, where this luxury is not provided and memory management is part of the developer’s duties.

It turns out JavaScript (via the browsers that implement it) uses a mark-and-sweep approach to garbage collection. My understanding of this is, the Garbage Collector basically walks the entire dependency tree, visiting each “reachable” object (meaning an object that is referenced by another object). The GC dumps all the unreachable objects

Read more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management

A quick example, and reminder that JS objects are passed by reference:

var a = [1,2,3];
var b = a;
b.pop();
console.log(a);
// [1,2]

a and b are both pointing at the same array object in memory. They are both references to that object.

var a = "bob";
var b = "same";

Now the reference to the array has been lost and it can be safely erased from memory.