on-internet
← Community

JS delete operator and garbage collection

Stack Overflow ·

I'm a bit confused with JavaScript's delete operator. Take the following piece of code: var obj = { helloText: "Hello World!" }; var foo = obj; delete obj; After this piece of code has been executed, obj is null, but foo still refers to an object exactly like obj. I'm guessing this object is the same object that foo pointed to. This confuses me, because I expected that writing delete obj deleted the object that obj was pointing to in memory—not just the variable obj. Is this because JavaScript's Garbage Collector is working on a retain/release basis, so that if I didn't have any other variable

Comments

Sign in or sign up to comment and upvote.

No comments yet.