Lets say I have defined an object.
let obj = {
"key": "prop"
}
And make use of this object reference in an array
const arr = [ obj ]
And now I make the object (obj) reference to null.
obj = null
So basically I have removed the reference of object (obj) which I have declared. So now if access Array (arr) items, I should get null as item. Not the object (obj) which I have defined earlier.
console.log(arr) // [ { "key": "prop" } ]
console.log(obj) // null
I am not able to get it exactly why is this happening. I have been asked this question in an interview.
I also found out that if we use Set instead of Array, then obj will be defined as null in WeakSet. So that means, it is garbage collected in this case at least.